From e55334e28138c434f6960ba5ad8efbd9939a450c Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 15:39:40 +0100 Subject: [PATCH 1/9] fix handling of generic alias --- atom/typing_utils.py | 31 ++++++++++++++++++++++++++--- tests/test_atom_from_annotations.py | 23 +++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/atom/typing_utils.py b/atom/typing_utils.py index b5da29d3..3dc5e37d 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -111,6 +111,31 @@ def _resolve_type_alias(kind: Any, seen: set[int] | None = None) -> Any: return _resolve_type_alias(kind.__value__, seen) +def _resolve_generic_origin(kind: Any) -> Any: + """Resolve alias-like generic origins to their underlying runtime type.""" + seen: set[int] = set() + current = kind + + while current is not None and id(current) not in seen: + seen.add(id(current)) + origin = get_origin(current) + if origin is None: + origin = getattr(current, "__origin__", None) + if origin is not None: + if isinstance(origin, type): + return origin + current = origin + continue + + value = getattr(current, "__value__", None) + if value is not None and value is not current: + current = value + continue + return None + + return None + + def _extract_types(kind: TypeLike) -> Tuple[type, ...]: """Extract a tuple of types from a type-like object""" kind = _resolve_type_alias(kind) @@ -120,13 +145,13 @@ def _extract_types(kind: TypeLike) -> Tuple[type, ...]: ) ret: List[Any] - if isinstance(kind, GENERICS): + origin = _resolve_generic_origin(kind) + if origin is not None or isinstance(kind, GENERICS): args = get_args(kind) - origin = get_origin(kind) if origin is Union: ret = list(chain.from_iterable(extract_types(a) for a in args)) else: - ret = [origin] + ret = [origin or kind] elif UNION and isinstance(kind, UNION): ret = list(chain.from_iterable(extract_types(a) for a in get_args(kind))) else: diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index eac429f9..09a800c3 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -125,6 +125,29 @@ class A(Atom, use_annotations=True, type_containers=1): assert isinstance(A.a.item, Int) +def test_generic_type_alias_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + from typing import Generic + + S = TypeVar("S") + T = TypeVar("T") + + class ndarray(Generic[S, T]): + pass + + NDArray = TypeAliasType("NDArray", ndarray[tuple[int, ...], T], type_params=(T,)) + + class A(Atom, use_annotations=True): + a: NDArray[int] + + assert isinstance(A.a, Typed) + assert A.a.validate_mode[1] is ndarray + + def test_reject_bare_member_annotated_member(): with pytest.raises(ValueError) as e: From c1c2583bd54760dccfc617575613cf1fc96d9841 Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 15:39:53 +0100 Subject: [PATCH 2/9] update release workflow --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4107f3ec..cbfc0da6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -114,7 +114,7 @@ jobs: path: dist merge-multiple: true - name: Sign the dists with Sigstore - uses: sigstore/gh-action-sigstore-python@v3.0.1 + uses: sigstore/gh-action-sigstore-python@v3.4.0 with: inputs: >- ./dist/*.tar.gz From cd01f0e7a40501919f5e13324cfa51e78d3f843b Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 16:07:39 +0100 Subject: [PATCH 3/9] fix handling of Optional on 3.11 --- atom/typing_utils.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/atom/typing_utils.py b/atom/typing_utils.py index 3dc5e37d..99f8003f 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -112,7 +112,7 @@ def _resolve_type_alias(kind: Any, seen: set[int] | None = None) -> Any: def _resolve_generic_origin(kind: Any) -> Any: - """Resolve alias-like generic origins to their underlying runtime type.""" + """Resolve alias-backed generic origins to their runtime type.""" seen: set[int] = set() current = kind @@ -121,16 +121,26 @@ def _resolve_generic_origin(kind: Any) -> Any: origin = get_origin(current) if origin is None: origin = getattr(current, "__origin__", None) - if origin is not None: - if isinstance(origin, type): - return origin - current = origin + + if origin is None: + value = getattr(current, "__value__", None) + if value is not None and value is not current: + current = value + continue + return None + + if _TYPE_ALIAS_TYPES and isinstance(origin, _TYPE_ALIAS_TYPES): + current = origin.__value__ continue - value = getattr(current, "__value__", None) - if value is not None and value is not current: + if isinstance(origin, type): + return origin + + value = getattr(origin, "__value__", None) + if value is not None and value is not origin: current = value continue + return None return None @@ -145,17 +155,16 @@ def _extract_types(kind: TypeLike) -> Tuple[type, ...]: ) ret: List[Any] - origin = _resolve_generic_origin(kind) - if origin is not None or isinstance(kind, GENERICS): - args = get_args(kind) - if origin is Union: - ret = list(chain.from_iterable(extract_types(a) for a in args)) - else: - ret = [origin or kind] - elif UNION and isinstance(kind, UNION): + origin = get_origin(kind) + if origin is Union or (UNION and isinstance(kind, UNION)): ret = list(chain.from_iterable(extract_types(a) for a in get_args(kind))) else: - ret = [kind] + if origin is not None and not isinstance(origin, type): + origin = _resolve_generic_origin(kind) + if origin is not None or isinstance(kind, GENERICS): + ret = [origin or kind] + else: + ret = [kind] extracted: List[type] = [] for t in ret: From 1da1695886419e0c21e5953579ed37f6d61d8ab7 Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 16:19:47 +0100 Subject: [PATCH 4/9] simplify logic and add tests to improve coverage --- atom/typing_utils.py | 3 --- tests/test_atom_from_annotations.py | 38 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/atom/typing_utils.py b/atom/typing_utils.py index 99f8003f..70096a1b 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -119,8 +119,6 @@ def _resolve_generic_origin(kind: Any) -> Any: while current is not None and id(current) not in seen: seen.add(id(current)) origin = get_origin(current) - if origin is None: - origin = getattr(current, "__origin__", None) if origin is None: value = getattr(current, "__value__", None) @@ -148,7 +146,6 @@ def _resolve_generic_origin(kind: Any) -> Any: def _extract_types(kind: TypeLike) -> Tuple[type, ...]: """Extract a tuple of types from a type-like object""" - kind = _resolve_type_alias(kind) if isinstance(kind, _INVALID_TYPES): raise TypeError( f"Str-based annotations ({kind!r}) are not supported in atom Members." diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index 09a800c3..c127e485 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -125,6 +125,21 @@ class A(Atom, use_annotations=True, type_containers=1): assert isinstance(A.a.item, Int) +def test_optional_type_alias_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + Alias = TypeAliasType("Alias", Optional[int]) + + class A(Atom, use_annotations=True): + a: Alias + + assert isinstance(A.a, Typed) + assert A.a.validate_mode[1] is int + + def test_generic_type_alias_in_annotation(): try: from typing_extensions import TypeAliasType @@ -147,6 +162,29 @@ class A(Atom, use_annotations=True): assert isinstance(A.a, Typed) assert A.a.validate_mode[1] is ndarray +def test_generic_type_alias_inside_generic_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + from typing import Generic + + S = TypeVar("S") + T = TypeVar("T") + + class ndarray(Generic[S, T]): + pass + + NDArray = TypeAliasType("NDArray", ndarray[tuple[int, ...], T], type_params=(T,)) + + class A(Atom, use_annotations=True): + a: list[NDArray[int]] + + assert isinstance(A.a, List) + assert isinstance(A.a.validate_mode[1], Typed) + assert A.a.validate_mode[1].validate_mode[1] is ndarray + def test_reject_bare_member_annotated_member(): with pytest.raises(ValueError) as e: From 1020d6e0826c55b732a6eb7b35b941836e48fa44 Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 16:21:23 +0100 Subject: [PATCH 5/9] format --- tests/test_atom_from_annotations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index c127e485..cc431379 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -162,6 +162,7 @@ class A(Atom, use_annotations=True): assert isinstance(A.a, Typed) assert A.a.validate_mode[1] is ndarray + def test_generic_type_alias_inside_generic_in_annotation(): try: from typing_extensions import TypeAliasType From 744c77be2cc44915166b2781fa2d91a530589bac Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 18:07:41 +0100 Subject: [PATCH 6/9] more tests and remove unused code --- atom/typing_utils.py | 27 +-------------------------- tests/test_atom_from_annotations.py | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/atom/typing_utils.py b/atom/typing_utils.py index 70096a1b..27979880 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -97,20 +97,6 @@ class ChangeDict(_ChangeDict, total=False): reverse: bool # operation in ("sort") -def _resolve_type_alias(kind: Any, seen: set[int] | None = None) -> Any: - """Resolve type aliases to their underlying annotation.""" - if not _TYPE_ALIAS_TYPES or not isinstance(kind, _TYPE_ALIAS_TYPES): - return kind - - if seen is None: - seen = set() - kind_id = id(kind) - if kind_id in seen: - raise TypeError(f"Cyclic type alias {kind!r} is not supported.") - seen.add(kind_id) - return _resolve_type_alias(kind.__value__, seen) - - def _resolve_generic_origin(kind: Any) -> Any: """Resolve alias-backed generic origins to their runtime type.""" seen: set[int] = set() @@ -120,13 +106,7 @@ def _resolve_generic_origin(kind: Any) -> Any: seen.add(id(current)) origin = get_origin(current) - if origin is None: - value = getattr(current, "__value__", None) - if value is not None and value is not current: - current = value - continue - return None - + # Extract a type alias if _TYPE_ALIAS_TYPES and isinstance(origin, _TYPE_ALIAS_TYPES): current = origin.__value__ continue @@ -134,11 +114,6 @@ def _resolve_generic_origin(kind: Any) -> Any: if isinstance(origin, type): return origin - value = getattr(origin, "__value__", None) - if value is not None and value is not origin: - current = value - continue - return None return None diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index cc431379..73d5cfda 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -125,19 +125,32 @@ class A(Atom, use_annotations=True, type_containers=1): assert isinstance(A.a.item, Int) -def test_optional_type_alias_in_annotation(): +def test_bare_type_alias_in_annotation(): try: from typing_extensions import TypeAliasType except ImportError: pytest.skip("typing_extensions.TypeAliasType is unavailable") - Alias = TypeAliasType("Alias", Optional[int]) + Alias = TypeAliasType("Alias", int) class A(Atom, use_annotations=True): a: Alias - assert isinstance(A.a, Typed) - assert A.a.validate_mode[1] is int + assert isinstance(A.a, Int) + +def test_type_alias_to_union_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + Alias = TypeAliasType("Alias", int | str) + + class A(Atom, use_annotations=True): + a: Alias + + assert isinstance(A.a, Instance) + assert A.a.validate_mode[1] == (int, str) def test_generic_type_alias_in_annotation(): @@ -162,6 +175,12 @@ class A(Atom, use_annotations=True): assert isinstance(A.a, Typed) assert A.a.validate_mode[1] is ndarray + class B(Atom, use_annotations=True): + a: NDArray[int | str] + + assert isinstance(B.a, Typed) + assert B.a.validate_mode[1] is ndarray + def test_generic_type_alias_inside_generic_in_annotation(): try: From 43bce0f7f1d42163d8fdf3d0bfc3791ad751bdb3 Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Wed, 4 Nov 2026 09:48:12 +0100 Subject: [PATCH 7/9] cleaner code and more tests --- atom/typing_utils.py | 46 +++++++++++++++++++---------- tests/test_atom_from_annotations.py | 33 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/atom/typing_utils.py b/atom/typing_utils.py index 27979880..42f4705e 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -97,26 +97,37 @@ class ChangeDict(_ChangeDict, total=False): reverse: bool # operation in ("sort") -def _resolve_generic_origin(kind: Any) -> Any: - """Resolve alias-backed generic origins to their runtime type.""" - seen: set[int] = set() +def _resolve_type_alias(kind: Any, seen: set[int] | None = None) -> Any: + """Resolve a type alias down to its underlying runtime value.""" + seen = set() if seen is None else seen current = kind - while current is not None and id(current) not in seen: + while True: + if id(current) in seen: + raise RuntimeError( + f"Failed to resolve type alias for {kind}, which is recursive." + ) # pragma: no cover seen.add(id(current)) - origin = get_origin(current) - # Extract a type alias - if _TYPE_ALIAS_TYPES and isinstance(origin, _TYPE_ALIAS_TYPES): - current = origin.__value__ + if _TYPE_ALIAS_TYPES and isinstance(current, _TYPE_ALIAS_TYPES): + current = current.__value__ continue - if isinstance(origin, type): - return origin + return current - return None - return None +def _resolve_generic_origin(kind: Any, seen: set[int] | None = None) -> Any: + """Resolve alias-backed generic origins to their concrete runtime type.""" + seen = set() if seen is None else seen + current = _resolve_type_alias(kind, seen) + + while True: + origin = get_origin(current) + if origin is None: + return current if isinstance(current, type) else None + if isinstance(origin, type): + return origin + current = _resolve_type_alias(origin, seen) def _extract_types(kind: TypeLike) -> Tuple[type, ...]: @@ -126,15 +137,18 @@ def _extract_types(kind: TypeLike) -> Tuple[type, ...]: f"Str-based annotations ({kind!r}) are not supported in atom Members." ) + kind = _resolve_type_alias(kind) + ret: List[Any] origin = get_origin(kind) if origin is Union or (UNION and isinstance(kind, UNION)): ret = list(chain.from_iterable(extract_types(a) for a in get_args(kind))) else: - if origin is not None and not isinstance(origin, type): - origin = _resolve_generic_origin(kind) - if origin is not None or isinstance(kind, GENERICS): - ret = [origin or kind] + resolved_origin = _resolve_generic_origin(kind) + if resolved_origin is not None: + ret = [resolved_origin] + elif isinstance(kind, GENERICS): + ret = [kind] else: ret = [kind] diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index 73d5cfda..7600f924 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -153,6 +153,39 @@ class A(Atom, use_annotations=True): assert A.a.validate_mode[1] == (int, str) +def test_alias_of_alias_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + Inner = TypeAliasType("Inner", int | str) + Outer = TypeAliasType("Outer", Inner) + + class A(Atom, use_annotations=True): + a: Outer + + assert isinstance(A.a, Instance) + assert A.a.validate_mode[1] == (int, str) + + +def test_nested_type_alias_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + Inner = TypeAliasType("Inner", int | str) + Outer = TypeAliasType("Outer", list[Inner]) + + class A(Atom, use_annotations=True, type_containers=1): + a: Outer + + assert isinstance(A.a, List) + assert isinstance(A.a.item, Instance) + assert A.a.item.validate_mode[1] == (int, str) + + def test_generic_type_alias_in_annotation(): try: from typing_extensions import TypeAliasType From 892f755e0f2ec8de75ecd9244b4385697e7ff2ed Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Wed, 4 Nov 2026 09:54:34 +0100 Subject: [PATCH 8/9] extra test for nested chain --- tests/test_atom_from_annotations.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index 7600f924..66d90333 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -169,6 +169,24 @@ class A(Atom, use_annotations=True): assert A.a.validate_mode[1] == (int, str) +def test_deep_alias_chain_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + Inner = TypeAliasType("Inner", int | str) + Middle = TypeAliasType("Middle", Inner) + Outer = TypeAliasType("Outer", list[Middle]) + + class A(Atom, use_annotations=True, type_containers=1): + a: Outer + + assert isinstance(A.a, List) + assert isinstance(A.a.item, Instance) + assert A.a.item.validate_mode[1] == (int, str) + + def test_nested_type_alias_in_annotation(): try: from typing_extensions import TypeAliasType From 60cbbe48ebecce46d649307377074fb296efc95b Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Wed, 4 Nov 2026 09:55:42 +0100 Subject: [PATCH 9/9] lint-format --- tests/test_atom_from_annotations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_atom_from_annotations.py b/tests/test_atom_from_annotations.py index 66d90333..fd69d3bb 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -138,6 +138,7 @@ class A(Atom, use_annotations=True): assert isinstance(A.a, Int) + def test_type_alias_to_union_in_annotation(): try: from typing_extensions import TypeAliasType