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:
Takeshi KOMIYA 2021-12-24 10:21:11 +09:00
parent 94cbce69da
commit 0a5783f75b
2 changed files with 5 additions and 3 deletions

View File

@ -105,6 +105,8 @@ def type_to_xref(target: str, env: BuildEnvironment = None, suppress_prefix: boo
text = target.split('.')[-1] text = target.split('.')[-1]
elif suppress_prefix: elif suppress_prefix:
text = target.split('.')[-1] text = target.split('.')[-1]
elif target.startswith('typing.'):
text = target[7:]
else: else:
text = target text = target
@ -1488,7 +1490,7 @@ def builtin_resolver(app: Sphinx, env: BuildEnvironment,
return None return None
elif node.get('reftype') in ('class', 'obj') and node.get('reftarget') == 'None': elif node.get('reftype') in ('class', 'obj') and node.get('reftarget') == 'None':
return contnode return contnode
elif node.get('reftype') in ('class', 'exc'): elif node.get('reftype') in ('class', 'obj', 'exc'):
reftarget = node.get('reftarget') reftarget = node.get('reftarget')
if inspect.isclass(getattr(builtins, reftarget, None)): if inspect.isclass(getattr(builtins, reftarget, None)):
# built-in class # built-in class

View File

@ -350,7 +350,7 @@ def test_parse_annotation(app):
# Literal type makes an object-reference (not a class reference) # Literal type makes an object-reference (not a class reference)
doctree = _parse_annotation("typing.Literal['a', 'b']", app.env) 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_punctuation, "["],
[desc_sig_literal_string, "'a'"], [desc_sig_literal_string, "'a'"],
[desc_sig_punctuation, ","], [desc_sig_punctuation, ","],
@ -384,7 +384,7 @@ def test_parse_annotation_Literal(app):
[desc_sig_punctuation, "]"])) [desc_sig_punctuation, "]"]))
doctree = _parse_annotation("typing.Literal[0, 1, 'abc']", app.env) 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_punctuation, "["],
[desc_sig_literal_number, "0"], [desc_sig_literal_number, "0"],
[desc_sig_punctuation, ","], [desc_sig_punctuation, ","],