Skip to content

Commit 0d69974

Browse files
GeeTransitAWhetter
authored andcommitted
Preserve strings inside Literal type annotations
1 parent 34a9670 commit 0d69974

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

autoapi/mappers/python/astroid_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,20 @@ def _resolve_annotation(annotation):
416416
# astroid.Index was removed in astroid v3
417417
if hasattr(astroid, "Index") and isinstance(slice_node, astroid.Index):
418418
slice_node = slice_node.value
419-
if isinstance(slice_node, astroid.Tuple):
419+
if value == "Literal":
420+
if isinstance(slice_node, astroid.Tuple):
421+
elts = slice_node.elts
422+
else:
423+
elts = [slice_node]
424+
slice_ = ", ".join(
425+
(
426+
elt.as_string()
427+
if isinstance(elt, astroid.Const)
428+
else _resolve_annotation(elt)
429+
)
430+
for elt in elts
431+
)
432+
elif isinstance(slice_node, astroid.Tuple):
420433
slice_ = ", ".join(_resolve_annotation(elt) for elt in slice_node.elts)
421434
else:
422435
slice_ = _resolve_annotation(slice_node)

docs/changes/423.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preserve strings inside Literal type annotations

tests/test_astroid_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ def func(
209209
),
210210
("a: int, *args, b: str, **kwargs", "a: int, *args, b: str, **kwargs"),
211211
("a: 'A'", "a: A"),
212+
("a: Literal[1]", "a: Literal[1]"),
213+
("a: Literal['x']", "a: Literal['x']"),
214+
("a: Literal['x', 'y', 'z']", "a: Literal['x', 'y', 'z']"),
212215
],
213216
)
214217
def test_format_args(self, signature, expected):

0 commit comments

Comments
 (0)