Skip to content

Commit d78a493

Browse files
committed
Python Domain: Avoid fuzzy match when looing up attrs as type annotations
The should prevent matches on class attributes when the type is a buildin such as list, set or type
1 parent cc7c6f4 commit d78a493

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

sphinx/domains/python/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,11 +945,12 @@ def resolve_xref(
945945
if not matches and type == 'class':
946946
# fallback to data/attr (for type aliases)
947947
# type aliases are documented as data/attr but referenced as class
948-
matches = self.find_obj(env, modname, clsname, target, 'data', searchmode)
948+
# Use searchmode=0 (exact match only) to avoid fuzzy matching
949+
# that could incorrectly match class attributes (e.g. D.list)
950+
# when resolving builtin type annotations (e.g. list[int]).
951+
matches = self.find_obj(env, modname, clsname, target, 'data', 0)
949952
if not matches:
950-
matches = self.find_obj(
951-
env, modname, clsname, target, 'attr', searchmode
952-
)
953+
matches = self.find_obj(env, modname, clsname, target, 'attr', 0)
953954
if not matches and type == 'attr':
954955
# fallback to meth (for property; Sphinx 2.4.x)
955956
# this ensures that `:attr:` role continues to refer to the old property entry

0 commit comments

Comments
 (0)