Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve strings inside Literal type annotations #423

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion autoapi/mappers/python/astroid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/changes/423.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve strings inside Literal type annotations
3 changes: 3 additions & 0 deletions tests/test_astroid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ def func(
),
("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):
Expand Down
Loading