mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
py domain: Suppress the leading "typing." module from typehints
To support creating hyperlinks to container types naturally, py domain should take fully-qualified typehints for them. But nobody wants to show "typing." module name on the signature. So this suppresses them automatically.
This commit is contained in:
parent
94cbce69da
commit
0a5783f75b
@ -105,6 +105,8 @@ def type_to_xref(target: str, env: BuildEnvironment = None, suppress_prefix: boo
|
||||
text = target.split('.')[-1]
|
||||
elif suppress_prefix:
|
||||
text = target.split('.')[-1]
|
||||
elif target.startswith('typing.'):
|
||||
text = target[7:]
|
||||
else:
|
||||
text = target
|
||||
|
||||
@ -1488,7 +1490,7 @@ def builtin_resolver(app: Sphinx, env: BuildEnvironment,
|
||||
return None
|
||||
elif node.get('reftype') in ('class', 'obj') and node.get('reftarget') == 'None':
|
||||
return contnode
|
||||
elif node.get('reftype') in ('class', 'exc'):
|
||||
elif node.get('reftype') in ('class', 'obj', 'exc'):
|
||||
reftarget = node.get('reftarget')
|
||||
if inspect.isclass(getattr(builtins, reftarget, None)):
|
||||
# built-in class
|
||||
|
@ -350,7 +350,7 @@ def test_parse_annotation(app):
|
||||
|
||||
# Literal type makes an object-reference (not a class reference)
|
||||
doctree = _parse_annotation("typing.Literal['a', 'b']", app.env)
|
||||
assert_node(doctree, ([pending_xref, "typing.Literal"],
|
||||
assert_node(doctree, ([pending_xref, "Literal"],
|
||||
[desc_sig_punctuation, "["],
|
||||
[desc_sig_literal_string, "'a'"],
|
||||
[desc_sig_punctuation, ","],
|
||||
@ -384,7 +384,7 @@ def test_parse_annotation_Literal(app):
|
||||
[desc_sig_punctuation, "]"]))
|
||||
|
||||
doctree = _parse_annotation("typing.Literal[0, 1, 'abc']", app.env)
|
||||
assert_node(doctree, ([pending_xref, "typing.Literal"],
|
||||
assert_node(doctree, ([pending_xref, "Literal"],
|
||||
[desc_sig_punctuation, "["],
|
||||
[desc_sig_literal_number, "0"],
|
||||
[desc_sig_punctuation, ","],
|
||||
|
Loading…
Reference in New Issue
Block a user