diff --git a/autoapi/mappers/python/astroid_utils.py b/autoapi/mappers/python/astroid_utils.py index b4018a05..e647cfc2 100644 --- a/autoapi/mappers/python/astroid_utils.py +++ b/autoapi/mappers/python/astroid_utils.py @@ -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) diff --git a/docs/changes/423.bugfix b/docs/changes/423.bugfix new file mode 100644 index 00000000..0f45e837 --- /dev/null +++ b/docs/changes/423.bugfix @@ -0,0 +1 @@ +Preserve strings inside Literal type annotations \ No newline at end of file diff --git a/tests/test_astroid_utils.py b/tests/test_astroid_utils.py index 1e0d7251..e2116385 100644 --- a/tests/test_astroid_utils.py +++ b/tests/test_astroid_utils.py @@ -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):