Skip to content

Commit 52787de

Browse files
committed
Fix #9775: py domain: Literal typehint was converted to a cross reference
1 parent 4c91c03 commit 52787de

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ Bugs fixed
7676
* #9678: linkcheck: file extension was shown twice in warnings
7777
* #9697: py domain: An index entry with parens was registered for ``py:method``
7878
directive with ``:property:`` option
79+
* #9775: py domain: Literal typehint was converted to a cross reference when
80+
:confval:`autodoc_typehints='description'`
7981
* #9708: needs_extension failed to check double-digit version correctly
8082
* #9688: Fix :rst:dir:`code`` does not recognize ``:class:`` option
8183

sphinx/domains/python.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,21 @@ def make_xrefs(self, rolename: str, domain: str, target: str,
353353

354354
split_contnode = bool(contnode and contnode.astext() == target)
355355

356+
in_literal = False
356357
results = []
357358
for sub_target in filter(None, sub_targets):
358359
if split_contnode:
359360
contnode = nodes.Text(sub_target)
360361

361-
if delims_re.match(sub_target):
362+
if in_literal or delims_re.match(sub_target):
362363
results.append(contnode or innernode(sub_target, sub_target))
363364
else:
364365
results.append(self.make_xref(rolename, domain, sub_target,
365366
innernode, contnode, env, inliner, location))
366367

368+
if sub_target in ('Literal', 'typing.Literal'):
369+
in_literal = True
370+
367371
return results
368372

369373

tests/test_domain_py.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,42 @@ def test_info_field_list_piped_type(app):
11101110
**{"py:module": "example", "py:class": "Class"})
11111111

11121112

1113+
def test_info_field_list_Literal(app):
1114+
text = (".. py:module:: example\n"
1115+
".. py:class:: Class\n"
1116+
"\n"
1117+
" :param age: blah blah\n"
1118+
" :type age: Literal['foo', 'bar', 'baz']\n")
1119+
doctree = restructuredtext.parse(app, text)
1120+
1121+
assert_node(doctree,
1122+
(nodes.target,
1123+
addnodes.index,
1124+
addnodes.index,
1125+
[desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)],
1126+
[desc_addname, "example."],
1127+
[desc_name, "Class"])],
1128+
[desc_content, nodes.field_list, nodes.field, (nodes.field_name,
1129+
nodes.field_body)])]))
1130+
assert_node(doctree[3][1][0][0][1],
1131+
([nodes.paragraph, ([addnodes.literal_strong, "age"],
1132+
" (",
1133+
[pending_xref, addnodes.literal_emphasis, "Literal"],
1134+
[addnodes.literal_emphasis, "["],
1135+
[addnodes.literal_emphasis, "'foo'"],
1136+
[addnodes.literal_emphasis, ", "],
1137+
[addnodes.literal_emphasis, "'bar'"],
1138+
[addnodes.literal_emphasis, ", "],
1139+
[addnodes.literal_emphasis, "'baz'"],
1140+
[addnodes.literal_emphasis, "]"],
1141+
")",
1142+
" -- ",
1143+
"blah blah")],))
1144+
assert_node(doctree[3][1][0][0][1][0][2], pending_xref,
1145+
refdomain="py", reftype="class", reftarget="Literal",
1146+
**{"py:module": "example", "py:class": "Class"})
1147+
1148+
11131149
def test_info_field_list_var(app):
11141150
text = (".. py:class:: Class\n"
11151151
"\n"

0 commit comments

Comments
 (0)