Skip to content

Commit

Permalink
fix: Unwrap TypeAliasType for all nested types in a type graph
Browse files Browse the repository at this point in the history
- Fixes an issue where field types would fail to resolve to an actionable marshaller/unmarshaller if they were a `TypeAliasType`.
  • Loading branch information
seandstewart committed Oct 30, 2024
1 parent 4205569 commit c837838
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/typelib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def get_type_graph(t: type) -> graphlib.TopologicalSorter[TypeNode]:
resolve one level deep on each attempt, otherwise we will find ourselves stuck
in a closed loop which never terminates (infinite recursion).
"""
if inspection.istypealiastype(t):
t = t.__value__

graph: graphlib.TopologicalSorter = graphlib.TopologicalSorter()
root = TypeNode(t)
stack = collections.deque([root])
Expand All @@ -127,6 +130,8 @@ def get_type_graph(t: type) -> graphlib.TopologicalSorter[TypeNode]:
# If no type was provided, there's no reason to do further processing.
if child in (constants.empty, typing.Any):
continue
if inspection.istypealiastype(child):
child = child.__value__

# Only subscripted generics or non-stdlib types can be cyclic.
# i.e., we may get `str` or `datetime` any number of times,
Expand Down
10 changes: 10 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import enum
import typing

from typelib.py import compat


@dataclasses.dataclass
class RecursiveType:
Expand Down Expand Up @@ -83,3 +85,11 @@ class ParentIntersect:
@dataclasses.dataclass
class ChildIntersect:
b: int


ListAlias = compat.TypeAliasType("ListAlias", list[int])


@dataclasses.dataclass
class NestedTypeAliasType:
alias: ListAlias
11 changes: 11 additions & 0 deletions tests/unit/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from typelib import graph
from typelib.py import refs

from tests import models
from tests.models import NestedTypeAliasType


@dataclasses.dataclass
class Simple:
Expand Down Expand Up @@ -87,6 +90,14 @@ class NoTypes:
],
),
any_type=dict(given_type=NoTypes, expected_nodes=[graph.TypeNode(type=NoTypes)]),
nested_type_alias=dict(
given_type=models.NestedTypeAliasType,
expected_nodes=[
graph.TypeNode(type=int),
graph.TypeNode(type=list[int], var="alias"),
graph.TypeNode(type=NestedTypeAliasType),
],
),
)
@pytest.mark.skipif(sys.version_info < (3, 10), reason="py3.10+")
def test_static_order(given_type, expected_nodes):
Expand Down

0 comments on commit c837838

Please sign in to comment.