Skip to content

Commit

Permalink
py domain: Suppress the leading "typing." module from typehints
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tk0miya committed Dec 24, 2021
1 parent 94cbce6 commit 0a5783f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -1488,7 +1490,7 @@ def istyping(s: str) -> bool:
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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_domain_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ","],
Expand Down Expand Up @@ -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, ","],
Expand Down

0 comments on commit 0a5783f

Please sign in to comment.