Skip to content

Commit 5da68c3

Browse files
committed
Fix py domain: "typing" types are not hyperlinked in info-field-list
1 parent f7d9ea2 commit 5da68c3

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Bugs fixed
5959
* #9944: LaTeX: extra vertical whitespace for some nested declarations
6060
* #9940: LaTeX: Multi-function declaration in Python domain has cramped
6161
vertical spacing in latexpdf output
62+
* #10015: py domain: types under the "typing" module are not hyperlinked defined
63+
at info-field-list
6264
* #9390: texinfo: Do not emit labels inside footnotes
6365
* #9979: Error level messages were displayed as warning messages
6466

sphinx/domains/python.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -362,27 +362,27 @@ def make_xref(self, rolename: str, domain: str, target: str,
362362
result = super().make_xref(rolename, domain, target, # type: ignore
363363
innernode, contnode,
364364
env, inliner=None, location=None)
365-
result['refspecific'] = True
366-
result['py:module'] = env.ref_context.get('py:module')
367-
result['py:class'] = env.ref_context.get('py:class')
368-
if target.startswith(('.', '~')):
369-
prefix, result['reftarget'] = target[0], target[1:]
370-
if prefix == '.':
371-
text = target[1:]
372-
elif prefix == '~':
373-
text = target.split('.')[-1]
374-
for node in list(result.traverse(nodes.Text)):
375-
node.parent[node.parent.index(node)] = nodes.Text(text)
376-
break
377-
elif isinstance(result, pending_xref) and env.config.python_use_unqualified_type_names:
378-
children = result.children
379-
result.clear()
380-
381-
shortname = target.split('.')[-1]
382-
textnode = innernode('', shortname)
383-
contnodes = [pending_xref_condition('', '', textnode, condition='resolved'),
384-
pending_xref_condition('', '', *children, condition='*')]
385-
result.extend(contnodes)
365+
if isinstance(result, pending_xref):
366+
result['refspecific'] = True
367+
result['py:module'] = env.ref_context.get('py:module')
368+
result['py:class'] = env.ref_context.get('py:class')
369+
370+
reftype, reftarget, reftitle, _ = parse_reftarget(target)
371+
if reftarget != reftitle:
372+
result['reftype'] = reftype
373+
result['reftarget'] = reftarget
374+
375+
result.clear()
376+
result += innernode(reftitle, reftitle)
377+
elif env.config.python_use_unqualified_type_names:
378+
children = result.children
379+
result.clear()
380+
381+
shortname = target.split('.')[-1]
382+
textnode = innernode('', shortname)
383+
contnodes = [pending_xref_condition('', '', textnode, condition='resolved'),
384+
pending_xref_condition('', '', *children, condition='*')]
385+
result.extend(contnodes)
386386

387387
return result
388388

@@ -415,33 +415,15 @@ def make_xrefs(self, rolename: str, domain: str, target: str,
415415

416416

417417
class PyField(PyXrefMixin, Field):
418-
def make_xref(self, rolename: str, domain: str, target: str,
419-
innernode: Type[TextlikeNode] = nodes.emphasis,
420-
contnode: Node = None, env: BuildEnvironment = None,
421-
inliner: Inliner = None, location: Node = None) -> Node:
422-
if rolename == 'class' and target == 'None':
423-
# None is not a type, so use obj role instead.
424-
rolename = 'obj'
425-
426-
return super().make_xref(rolename, domain, target, innernode, contnode,
427-
env, inliner, location)
418+
pass
428419

429420

430421
class PyGroupedField(PyXrefMixin, GroupedField):
431422
pass
432423

433424

434425
class PyTypedField(PyXrefMixin, TypedField):
435-
def make_xref(self, rolename: str, domain: str, target: str,
436-
innernode: Type[TextlikeNode] = nodes.emphasis,
437-
contnode: Node = None, env: BuildEnvironment = None,
438-
inliner: Inliner = None, location: Node = None) -> Node:
439-
if rolename == 'class' and target == 'None':
440-
# None is not a type, so use obj role instead.
441-
rolename = 'obj'
442-
443-
return super().make_xref(rolename, domain, target, innernode, contnode,
444-
env, inliner, location)
426+
pass
445427

446428

447429
class PyObject(ObjectDescription[Tuple[str, str]]):

tests/test_domain_py.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,9 @@ def test_type_field(app):
11961196
text = (".. py:data:: var1\n"
11971197
" :type: .int\n"
11981198
".. py:data:: var2\n"
1199-
" :type: ~builtins.int\n")
1199+
" :type: ~builtins.int\n"
1200+
".. py:data:: var3\n"
1201+
" :type: typing.Optional[typing.Tuple[int, typing.Any]]\n")
12001202
doctree = restructuredtext.parse(app, text)
12011203
assert_node(doctree, (addnodes.index,
12021204
[desc, ([desc_signature, ([desc_name, "var1"],
@@ -1209,9 +1211,28 @@ def test_type_field(app):
12091211
[desc_annotation, ([desc_sig_punctuation, ':'],
12101212
desc_sig_space,
12111213
[pending_xref, "int"])])],
1214+
[desc_content, ()])],
1215+
addnodes.index,
1216+
[desc, ([desc_signature, ([desc_name, "var3"],
1217+
[desc_annotation, ([desc_sig_punctuation, ":"],
1218+
desc_sig_space,
1219+
[pending_xref, "Optional"],
1220+
[desc_sig_punctuation, "["],
1221+
[pending_xref, "Tuple"],
1222+
[desc_sig_punctuation, "["],
1223+
[pending_xref, "int"],
1224+
[desc_sig_punctuation, ","],
1225+
desc_sig_space,
1226+
[pending_xref, "Any"],
1227+
[desc_sig_punctuation, "]"],
1228+
[desc_sig_punctuation, "]"])])],
12121229
[desc_content, ()])]))
12131230
assert_node(doctree[1][0][1][2], pending_xref, reftarget='int', refspecific=True)
12141231
assert_node(doctree[3][0][1][2], pending_xref, reftarget='builtins.int', refspecific=False)
1232+
assert_node(doctree[5][0][1][2], pending_xref, reftarget='typing.Optional', refspecific=False)
1233+
assert_node(doctree[5][0][1][4], pending_xref, reftarget='typing.Tuple', refspecific=False)
1234+
assert_node(doctree[5][0][1][6], pending_xref, reftarget='int', refspecific=False)
1235+
assert_node(doctree[5][0][1][9], pending_xref, reftarget='typing.Any', refspecific=False)
12151236

12161237

12171238
@pytest.mark.sphinx(freshenv=True)

0 commit comments

Comments
 (0)