Skip to content

Commit e6674ec

Browse files
committed
Fix #7199: py domain: Add a new confval: python_use_unqualified_type_names
Add a new config variable: python_use_unqualified_type_names. If enabled, it goes to suppress the module name of the python reference if it can be resolved.
1 parent c58b2a1 commit e6674ec

File tree

7 files changed

+73
-8
lines changed

7 files changed

+73
-8
lines changed

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Deprecated
1313
Features added
1414
--------------
1515

16+
* #7199: py domain: Add :confval:`python_use_unqualified_type_names` to suppress
17+
the module name of the python reference if it can be resolved (experimental)
1618
* #7199: A new node, ``sphinx.addnodes.pending_xref_condition`` has been added.
1719
It can be used to choose appropriate content of the reference by conditions.
1820

doc/usage/configuration.rst

+11
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,17 @@ Options for the C++ domain
26322632

26332633
.. versionadded:: 1.5
26342634

2635+
Options for the Python domain
2636+
-----------------------------
2637+
2638+
.. confval:: python_use_unqualified_type_names
2639+
2640+
If true, suppress the module name of the python reference if it can be
2641+
resolved. The default is ``False``.
2642+
2643+
.. versionadded:: 3.4
2644+
2645+
.. note:: This configuration is still in experimental
26352646

26362647
Example of configuration file
26372648
=============================

sphinx/domains/python.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from docutils.parsers.rst import directives
2323

2424
from sphinx import addnodes
25-
from sphinx.addnodes import desc_signature, pending_xref
25+
from sphinx.addnodes import desc_signature, pending_xref, pending_xref_condition
2626
from sphinx.application import Sphinx
2727
from sphinx.builders import Builder
2828
from sphinx.deprecation import RemovedInSphinx40Warning, RemovedInSphinx50Warning
@@ -37,7 +37,7 @@
3737
from sphinx.util.docfields import Field, GroupedField, TypedField
3838
from sphinx.util.docutils import SphinxDirective
3939
from sphinx.util.inspect import signature_from_str
40-
from sphinx.util.nodes import make_id, make_refnode
40+
from sphinx.util.nodes import find_pending_xref_condition, make_id, make_refnode
4141
from sphinx.util.typing import TextlikeNode
4242

4343
if False:
@@ -91,7 +91,14 @@ def type_to_xref(text: str, env: BuildEnvironment = None) -> addnodes.pending_xr
9191
else:
9292
kwargs = {}
9393

94-
return pending_xref('', nodes.Text(text),
94+
if env.config.python_use_unqualified_type_names:
95+
shortname = text.split('.')[-1]
96+
contnodes = [pending_xref_condition('', shortname, condition='resolved'),
97+
pending_xref_condition('', text, condition='*')] # type: List[Node]
98+
else:
99+
contnodes = [nodes.Text(text)]
100+
101+
return pending_xref('', *contnodes,
95102
refdomain='py', reftype=reftype, reftarget=text, **kwargs)
96103

97104

@@ -1315,7 +1322,15 @@ def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder
13151322
if obj[2] == 'module':
13161323
return self._make_module_refnode(builder, fromdocname, name, contnode)
13171324
else:
1318-
return make_refnode(builder, fromdocname, obj[0], obj[1], contnode, name)
1325+
# determine the content of the reference by conditions
1326+
content = find_pending_xref_condition(node, 'resolved')
1327+
if content:
1328+
children = content.children
1329+
else:
1330+
# if not found, use contnode
1331+
children = [contnode]
1332+
1333+
return make_refnode(builder, fromdocname, obj[0], obj[1], children, name)
13191334

13201335
def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder,
13211336
target: str, node: pending_xref, contnode: Element
@@ -1332,9 +1347,17 @@ def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Bui
13321347
self._make_module_refnode(builder, fromdocname,
13331348
name, contnode)))
13341349
else:
1350+
# determine the content of the reference by conditions
1351+
content = find_pending_xref_condition(node, 'resolved')
1352+
if content:
1353+
children = content.children
1354+
else:
1355+
# if not found, use contnode
1356+
children = [contnode]
1357+
13351358
results.append(('py:' + self.role_for_objtype(obj[2]),
13361359
make_refnode(builder, fromdocname, obj[0], obj[1],
1337-
contnode, name)))
1360+
children, name)))
13381361
return results
13391362

13401363
def _make_module_refnode(self, builder: Builder, fromdocname: str, name: str,
@@ -1397,6 +1420,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
13971420
app.setup_extension('sphinx.directives')
13981421

13991422
app.add_domain(PythonDomain)
1423+
app.add_config_value('python_use_unqualified_type_names', False, 'env')
14001424
app.connect('object-description-transform', filter_meta_fields)
14011425
app.connect('missing-reference', builtin_resolver, priority=900)
14021426

sphinx/util/nodes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import re
1212
import unicodedata
1313
import warnings
14-
from typing import Any, Callable, Iterable, List, Set, Tuple, cast
14+
from typing import Any, Callable, Iterable, List, Set, Tuple, Union, cast
1515

1616
from docutils import nodes
1717
from docutils.nodes import Element, Node
@@ -549,7 +549,7 @@ def find_pending_xref_condition(node: addnodes.pending_xref, condition: str) ->
549549

550550

551551
def make_refnode(builder: "Builder", fromdocname: str, todocname: str, targetid: str,
552-
child: Node, title: str = None) -> nodes.reference:
552+
child: Union[Node, List[Node]], title: str = None) -> nodes.reference:
553553
"""Shortcut to create a reference node."""
554554
node = nodes.reference('', '', internal=True)
555555
if fromdocname == todocname and targetid:
@@ -562,7 +562,7 @@ def make_refnode(builder: "Builder", fromdocname: str, todocname: str, targetid:
562562
node['refuri'] = builder.get_relative_uri(fromdocname, todocname)
563563
if title:
564564
node['reftitle'] = title
565-
node.append(child)
565+
node += child
566566
return node
567567

568568

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python_use_unqualified_type_names = True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
domain-py-smart_reference
2+
=========================
3+
4+
.. py:class:: Name
5+
:module: foo
6+
7+
8+
.. py:function:: hello(name: foo.Name, age: foo.Age)

tests/test_domain_py.py

+19
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,25 @@ def test_noindexentry(app):
906906
assert_node(doctree[2], addnodes.index, entries=[])
907907

908908

909+
@pytest.mark.sphinx('html', testroot='domain-py-python_use_unqualified_type_names')
910+
def test_python_python_use_unqualified_type_names(app, status, warning):
911+
app.build()
912+
content = (app.outdir / 'index.html').read_text()
913+
assert ('<span class="n"><a class="reference internal" href="#foo.Name" title="foo.Name">'
914+
'Name</a></span>' in content)
915+
assert '<span class="n">foo.Age</span>' in content
916+
917+
918+
@pytest.mark.sphinx('html', testroot='domain-py-python_use_unqualified_type_names',
919+
confoverrides={'python_use_unqualified_type_names': False})
920+
def test_python_python_use_unqualified_type_names_disabled(app, status, warning):
921+
app.build()
922+
content = (app.outdir / 'index.html').read_text()
923+
assert ('<span class="n"><a class="reference internal" href="#foo.Name" title="foo.Name">'
924+
'foo.Name</a></span>' in content)
925+
assert '<span class="n">foo.Age</span>' in content
926+
927+
909928
@pytest.mark.sphinx('dummy', testroot='domain-py-xref-warning')
910929
def test_warn_missing_reference(app, status, warning):
911930
app.build()

0 commit comments

Comments
 (0)