Skip to content

Commit

Permalink
Merge pull request #9289 from tk0miya/9268_python_use_unqualified_typ…
Browse files Browse the repository at this point in the history
…e_names

Close #9268: python_use_unqualified_type_names supports type field
  • Loading branch information
tk0miya committed Jun 3, 2021
2 parents 82dad44 + 90470b0 commit 01918fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions sphinx/domains/python.py
Expand Up @@ -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]
textnode = innernode('', shortname)
contnodes = [pending_xref_condition('', '', textnode, condition='resolved'),
pending_xref_condition('', '', *children, condition='*')]
result.extend(contnodes)

return result

def make_xrefs(self, rolename: str, domain: str, target: str,
Expand Down
Expand Up @@ -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)
6 changes: 6 additions & 0 deletions tests/test_domain_py.py
Expand Up @@ -1147,6 +1147,9 @@ def test_python_python_use_unqualified_type_names(app, status, warning):
assert ('<span class="n"><a class="reference internal" href="#foo.Name" title="foo.Name">'
'<span class="pre">Name</span></a></span>' in content)
assert '<span class="n"><span class="pre">foo.Age</span></span>' in content
assert ('<p><strong>name</strong> (<a class="reference internal" href="#foo.Name" '
'title="foo.Name"><em>Name</em></a>) – blah blah</p>' in content)
assert '<p><strong>age</strong> (<em>foo.Age</em>) – blah blah</p>' in content


@pytest.mark.sphinx('html', testroot='domain-py-python_use_unqualified_type_names',
Expand All @@ -1157,6 +1160,9 @@ def test_python_python_use_unqualified_type_names_disabled(app, status, warning)
assert ('<span class="n"><a class="reference internal" href="#foo.Name" title="foo.Name">'
'<span class="pre">foo.Name</span></a></span>' in content)
assert '<span class="n"><span class="pre">foo.Age</span></span>' in content
assert ('<p><strong>name</strong> (<a class="reference internal" href="#foo.Name" '
'title="foo.Name"><em>foo.Name</em></a>) – blah blah</p>' in content)
assert '<p><strong>age</strong> (<em>foo.Age</em>) – blah blah</p>' in content


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

0 comments on commit 01918fc

Please sign in to comment.