diff --git a/CHANGES b/CHANGES index 879ae64ebd..32cb7b4057 100644 --- a/CHANGES +++ b/CHANGES @@ -44,6 +44,8 @@ Features added * #9176: i18n: Emit a debug message if message catalog file not found under :confval:`locale_dirs` * #1874: py domain: Support union types using ``|`` in info-field-list +* #9268: py domain: :confval:`python_use_unqualified_type_names` supports type + field in info-field-list * #9097: Optimize the paralell build * #9131: Add :confval:`nitpick_ignore_regex` to ignore nitpicky warnings using regular expressions diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 7fb56c6353..3c2879485c 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -299,6 +299,16 @@ def make_xref(self, rolename: str, domain: str, target: str, for node in result.traverse(nodes.Text): node.parent[node.parent.index(node)] = nodes.Text(text) break + elif isinstance(result, pending_xref) and env.config.python_use_unqualified_type_names: + children = result.children + result.clear() + + shortname = target.split('.')[-1] + text = innernode('', shortname) + contnodes = [pending_xref_condition('', '', text, condition='resolved'), + pending_xref_condition('', '', *children, condition='*')] + result.extend(contnodes) + return result def make_xrefs(self, rolename: str, domain: str, target: str, diff --git a/tests/roots/test-domain-py-python_use_unqualified_type_names/index.rst b/tests/roots/test-domain-py-python_use_unqualified_type_names/index.rst index 599206d8c7..a6850a0f49 100644 --- a/tests/roots/test-domain-py-python_use_unqualified_type_names/index.rst +++ b/tests/roots/test-domain-py-python_use_unqualified_type_names/index.rst @@ -4,5 +4,9 @@ domain-py-smart_reference .. py:class:: Name :module: foo + :param name: blah blah + :type name: foo.Name + :param age: blah blah + :type age: foo.Age .. py:function:: hello(name: foo.Name, age: foo.Age) diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index e66f066d4d..2ee2d5f2dd 100644 --- a/tests/test_domain_py.py +++ b/tests/test_domain_py.py @@ -1147,6 +1147,9 @@ def test_python_python_use_unqualified_type_names(app, status, warning): assert ('' 'Name' in content) assert 'foo.Age' in content + assert ('

name (Name) – blah blah

' in content) + assert '

age (foo.Age) – blah blah

' in content @pytest.mark.sphinx('html', testroot='domain-py-python_use_unqualified_type_names', @@ -1157,6 +1160,9 @@ def test_python_python_use_unqualified_type_names_disabled(app, status, warning) assert ('' 'foo.Name' in content) assert 'foo.Age' in content + assert ('

name (foo.Name) – blah blah

' in content) + assert '

age (foo.Age) – blah blah

' in content @pytest.mark.sphinx('dummy', testroot='domain-py-xref-warning')