diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-03-06 11:32:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-06 11:32:27 +0300 |
commit | 0a3f897d1523236928e570d16c925a5ad08e1bf0 (patch) | |
tree | 82b26886df2038c109e5b2d752f7558ff679d41a /sphinx/domains | |
parent | 6886de29f23304457ba7c3cda3ec51e3f53ac208 (diff) | |
parent | 7f0b13af6e023382f8bfdaa0916c614a586ff05c (diff) |
Merge pull request #8432 from tk0miya/7119_pending_xref_condition
Fix #7119: Show type hint names unqualified when resolving succeeded
Diffstat (limited to 'sphinx/domains')
-rw-r--r-- | sphinx/domains/python.py | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 5e430a1d7..40a67f82c 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -22,7 +22,7 @@ from docutils.nodes import Element, Node from docutils.parsers.rst import directives from sphinx import addnodes -from sphinx.addnodes import desc_signature, pending_xref +from sphinx.addnodes import desc_signature, pending_xref, pending_xref_condition from sphinx.application import Sphinx from sphinx.builders import Builder from sphinx.deprecation import RemovedInSphinx50Warning @@ -37,7 +37,7 @@ from sphinx.util import logging from sphinx.util.docfields import Field, GroupedField, TypedField from sphinx.util.docutils import SphinxDirective from sphinx.util.inspect import signature_from_str -from sphinx.util.nodes import make_id, make_refnode +from sphinx.util.nodes import find_pending_xref_condition, make_id, make_refnode from sphinx.util.typing import TextlikeNode logger = logging.getLogger(__name__) @@ -92,7 +92,17 @@ def type_to_xref(text: str, env: BuildEnvironment = None) -> addnodes.pending_xr else: kwargs = {} - return pending_xref('', nodes.Text(text), + if env.config.python_use_unqualified_type_names: + # Note: It would be better to use qualname to describe the object to support support + # nested classes. But python domain can't access the real python object because this + # module should work not-dynamically. + shortname = text.split('.')[-1] + contnodes = [pending_xref_condition('', shortname, condition='resolved'), + pending_xref_condition('', text, condition='*')] # type: List[Node] + else: + contnodes = [nodes.Text(text)] + + return pending_xref('', *contnodes, refdomain='py', reftype=reftype, reftarget=text, **kwargs) @@ -1209,7 +1219,15 @@ class PythonDomain(Domain): if obj[2] == 'module': return self._make_module_refnode(builder, fromdocname, name, contnode) else: - return make_refnode(builder, fromdocname, obj[0], obj[1], contnode, name) + # determine the content of the reference by conditions + content = find_pending_xref_condition(node, 'resolved') + if content: + children = content.children + else: + # if not found, use contnode + children = [contnode] + + return make_refnode(builder, fromdocname, obj[0], obj[1], children, name) def resolve_any_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder, target: str, node: pending_xref, contnode: Element @@ -1226,9 +1244,17 @@ class PythonDomain(Domain): self._make_module_refnode(builder, fromdocname, name, contnode))) else: + # determine the content of the reference by conditions + content = find_pending_xref_condition(node, 'resolved') + if content: + children = content.children + else: + # if not found, use contnode + children = [contnode] + results.append(('py:' + self.role_for_objtype(obj[2]), make_refnode(builder, fromdocname, obj[0], obj[1], - contnode, name))) + children, name))) return results def _make_module_refnode(self, builder: Builder, fromdocname: str, name: str, @@ -1295,6 +1321,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.setup_extension('sphinx.directives') app.add_domain(PythonDomain) + app.add_config_value('python_use_unqualified_type_names', False, 'env') app.connect('object-description-transform', filter_meta_fields) app.connect('missing-reference', builtin_resolver, priority=900) |