diff --git a/CHANGES b/CHANGES index 2ce9ac36649..c8e8d0ecdd5 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,9 @@ Features added Bugs fixed ---------- +* #9577, #10088: Fix warning for duplicate Python references when using :any: + and autodoc + Testing -------- diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 145b62eda8c..b00ab8180c2 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -1339,6 +1339,7 @@ def find_obj(self, env: BuildEnvironment, modname: str, classname: str, matches = [(oname, self.objects[oname]) for oname in self.objects if oname.endswith(searchname) and self.objects[oname].objtype in objtypes] + else: # NOTE: searching for exact match, object type is not considered if name in self.objects: @@ -1413,6 +1414,11 @@ def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Bui # always search in "refspecific" mode with the :any: role matches = self.find_obj(env, modname, clsname, target, None, 1) + + if len(matches) > 1: + # Remove all duplicated matches + matches = [(name, obj) for name, obj in matches if not obj.aliased] + for name, obj in matches: if obj[2] == 'module': results.append(('py:mod', diff --git a/tests/roots/test-domain-py/canonical.rst b/tests/roots/test-domain-py/canonical.rst index eff783aadde..34becfdfb69 100644 --- a/tests/roots/test-domain-py/canonical.rst +++ b/tests/roots/test-domain-py/canonical.rst @@ -2,6 +2,9 @@ caninical ========= :py:class:`.Foo` +:any:`Foo` +:any:`module.Foo` +:any:`original.module.Foo` .. py:module:: canonical