Preserve strings inside Literal type annotations

pull/394/merge
George Zhang 3 months ago committed by Ashley Whetter
parent 34a96700ea
commit 0d69974c19

@ -416,7 +416,20 @@ def _resolve_annotation(annotation):
# astroid.Index was removed in astroid v3
if hasattr(astroid, "Index") and isinstance(slice_node, astroid.Index):
slice_node = slice_node.value
if isinstance(slice_node, astroid.Tuple):
if value == "Literal":
if isinstance(slice_node, astroid.Tuple):
elts = slice_node.elts
else:
elts = [slice_node]
slice_ = ", ".join(
(
elt.as_string()
if isinstance(elt, astroid.Const)
else _resolve_annotation(elt)
)
for elt in elts
)
elif isinstance(slice_node, astroid.Tuple):
slice_ = ", ".join(_resolve_annotation(elt) for elt in slice_node.elts)
else:
slice_ = _resolve_annotation(slice_node)

@ -0,0 +1 @@
Preserve strings inside Literal type annotations

@ -209,6 +209,9 @@ class TestAstroidUtils:
),
("a: int, *args, b: str, **kwargs", "a: int, *args, b: str, **kwargs"),
("a: 'A'", "a: A"),
("a: Literal[1]", "a: Literal[1]"),
("a: Literal['x']", "a: Literal['x']"),
("a: Literal['x', 'y', 'z']", "a: Literal['x', 'y', 'z']"),
],
)
def test_format_args(self, signature, expected):

Loading…
Cancel
Save