Skip to content

Commit b846a0c

Browse files
committed
Render type aliases properly rather than as an assignment
Closes #414
1 parent 5af3ffe commit b846a0c

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

autoapi/_objects.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ def __init__(self, *args, **kwargs):
351351
or annotation comment was not given.
352352
"""
353353

354+
def is_type_alias(self):
355+
return self.annotation in ("TypeAlias", "typing.TypeAlias")
356+
354357

355358
class PythonAttribute(PythonData):
356359
"""An object/class level attribute."""

autoapi/_parser.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,12 @@ def parse_typealias(self, node):
405405
):
406406
doc = doc_node.value.value
407407

408+
type_ = "data"
409+
if isinstance(
410+
node.scope(), astroid.nodes.ClassDef
411+
) or _astroid_utils.is_constructor(node.scope()):
412+
type_ = "attribute"
413+
408414
if isinstance(node.name, astroid.nodes.AssignName):
409415
name = node.name.name
410416
elif isinstance(node.name, astroid.nodes.AssignAttr):
@@ -413,7 +419,7 @@ def parse_typealias(self, node):
413419
return []
414420

415421
data = {
416-
"type": "data",
422+
"type": type_,
417423
"name": name,
418424
"qual_name": self._get_qual_name(name),
419425
"full_name": self._get_full_name(name),

autoapi/templates/python/data.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
{{ "=" * obj.id | length }}
55

66
{% endif %}
7-
.. py:{{ obj.type }}:: {% if is_own_page %}{{ obj.id }}{% else %}{{ obj.name }}{% endif %}
8-
{% if obj.annotation is not none %}
7+
.. py:{% if obj.is_type_alias() %}type{% else %}{{ obj.type }}{% endif %}:: {% if is_own_page %}{{ obj.id }}{% else %}{{ obj.name }}{% endif %}
8+
{% if obj.is_type_alias() %}
9+
{% if obj.value %}
10+
11+
:canonical: {{ obj.value }}
12+
{% endif %}
13+
{% else %}
14+
{% if obj.annotation is not none %}
915

1016
:type: {% if obj.annotation %} {{ obj.annotation }}{% endif %}
11-
{% endif %}
12-
{% if obj.value is not none %}
17+
{% endif %}
18+
{% if obj.value is not none %}
1319

14-
{% if obj.value.splitlines()|count > 1 %}
20+
{% if obj.value.splitlines()|count > 1 %}
1521
:value: Multiline-String
1622

1723
.. raw:: html
@@ -26,8 +32,9 @@
2632

2733
</details>
2834

29-
{% else %}
35+
{% else %}
3036
:value: {{ obj.value|truncate(100) }}
37+
{% endif %}
3138
{% endif %}
3239
{% endif %}
3340

docs/changes/414.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Render type aliases properly rather than as an assignment

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ dependencies = [
2929
'astroid>=3;python_version>="3.12"',
3030
"Jinja2",
3131
"PyYAML",
32-
"sphinx>=6.1.0",
32+
"sphinx>=7.4.0",
3333
'stdlib_list;python_version<"3.10"',
3434
]
3535
dynamic = ["version"]

tests/python/test_pyintegration.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,16 +747,12 @@ def test_integration(self, parse):
747747
alias = example_file.find(id="example.MyTypeAliasA")
748748
properties = alias.find_all(class_="property")
749749
assert len(properties) == 2
750-
annotation = properties[0].text
751-
assert annotation == ": TypeAlias"
752750
value = properties[1].text
753751
assert value == " = tuple[str, int]"
754752

755753
alias = example_file.find(id="example.MyTypeAliasB")
756754
properties = alias.find_all(class_="property")
757755
assert len(properties) == 2
758-
annotation = properties[0].text
759-
assert annotation == ": TypeAlias"
760756
value = properties[1].text
761757
assert value == " = tuple[str, int]"
762758

0 commit comments

Comments
 (0)