Skip to content

Render typing_extensions.TypeAlias like other type aliases #521

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions autoapi/_astroid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ def _resolve_annotation(annotation: astroid.nodes.NodeNG) -> str:

if resolved.startswith("typing."):
return resolved[len("typing.") :]
if resolved.startswith("typing_extensions."):
return resolved[len("typing_extensions.") :]

# Sphinx is capable of linking anything in the same module
# without needing a fully qualified path.
Expand Down
6 changes: 5 additions & 1 deletion autoapi/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,11 @@ def __init__(self, *args, **kwargs):
"""

def is_type_alias(self):
return self.annotation in ("TypeAlias", "typing.TypeAlias")
return self.annotation in (
"TypeAlias",
"typing.TypeAlias",
"typing_extensions.TypeAlias",
)


class PythonAttribute(PythonData):
Expand Down
6 changes: 5 additions & 1 deletion autoapi/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def _parse_assign(self, node):
value_node = assign_value[1]

annotation = _astroid_utils.get_assign_annotation(node)
if annotation in ("TypeAlias", "typing.TypeAlias"):
if annotation in (
"TypeAlias",
"typing.TypeAlias",
"typing_extensions.TypeAlias",
):
value = node.value.as_string()
elif isinstance(
value_node, astroid.nodes.ClassDef
Expand Down
4 changes: 4 additions & 0 deletions tests/python/pep695/example/example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from typing import TypeAlias
import typing
import typing_extensions

MyTypeAliasA: TypeAlias = tuple[str, int]
type MyTypeAliasB = tuple[str, int]
MyTypeAliasC: typing.TypeAlias = tuple[str, int]
MyTypeAliasD: typing_extensions.TypeAlias = tuple[str, int]
12 changes: 12 additions & 0 deletions tests/python/test_pyintegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,18 @@ def test_integration(self, parse):
value = properties[1].text
assert value == " = tuple[str, int]"

alias = example_file.find(id="example.MyTypeAliasC")
properties = alias.find_all(class_="property")
assert len(properties) == 2
value = properties[1].text
assert value == " = tuple[str, int]"

alias = example_file.find(id="example.MyTypeAliasD")
properties = alias.find_all(class_="property")
assert len(properties) == 2
value = properties[1].text
assert value == " = tuple[str, int]"


def test_napoleon_integration_loaded(builder, parse):
confoverrides = {
Expand Down
Loading