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

fix: Unwrap TypeAliasType for all nested types in a type graph #6

Merged
merged 2 commits into from
Oct 30, 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
5 changes: 5 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name: Make Documentation
on:
push:
branches: ["main"]
tags-ignore:
- '**'
release:
types:
- published

defaults:
run:
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ jobs:
with:
python-version: 3.x
runner: ubuntu-latest
- name: Compile Release Notes
run: make release-notes > release-notes.md
- name: Report Version
run: |
export "RELEASE_VERSION=v$(make report-version)";
export "TARGET_COMMITISH=$(git rev-list -n 1 "${RELEASE_VERSION}")";
echo "RELEASE_VERSION=${RELEASE_VERSION}" >> $GITHUB_ENV
echo "TARGET_COMMITISH=${TARGET_COMMITISH}" >> $GITHUB_ENV
- name: Download all the dists
uses: actions/download-artifact@v4
with:
Expand All @@ -108,14 +116,6 @@ jobs:
inputs: >-
./dist/*.tar.gz
./dist/*.whl
- name: Compile Release Notes
run: make release-notes > release-notes.md
- name: Report Version
run: |
export "RELEASE_VERSION=v$(make report-version)";
export "TARGET_COMMITISH=$(git rev-list -n 1 "${RELEASE_VERSION}")";
echo "RELEASE_VERSION=${RELEASE_VERSION}" >> $GITHUB_ENV
echo "TARGET_COMMITISH=${TARGET_COMMITISH}" >> $GITHUB_ENV
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Validate
on:
push:
branches:
- '**'
tags-ignore:
- '**'

jobs:
ci:
Expand Down
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
Loading