From 732b30c072a705d0f1eac4df205bd7662d00009f Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 13:55:47 +0100 Subject: [PATCH 1/4] add support for type alias in annotations used to generate a member --- atom/meta/annotation_utils.py | 13 ++++++++++++- atom/typing_utils.py | 30 +++++++++++++++++++++++++++++ tests/test_atom_from_annotations.py | 15 +++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/atom/meta/annotation_utils.py b/atom/meta/annotation_utils.py index 22f9d522..25ece445 100644 --- a/atom/meta/annotation_utils.py +++ b/atom/meta/annotation_utils.py @@ -20,7 +20,13 @@ from ..subclass import Subclass from ..tuple import FixedTuple, Tuple as ATuple from ..typed import Typed -from ..typing_utils import extract_types, get_args, get_origin, is_optional +from ..typing_utils import ( + _TYPE_ALIAS_TYPES, + extract_types, + get_args, + get_origin, + is_optional, +) from .member_modifiers import set_default _NO_DEFAULT = object() @@ -44,6 +50,11 @@ def generate_member_from_type_or_generic( type_generic: Any, default: Any, annotate_type_containers: int ) -> Member: """Generate a member from a type or generic alias.""" + if _TYPE_ALIAS_TYPES and isinstance(type_generic, _TYPE_ALIAS_TYPES): + return generate_member_from_type_or_generic( + type_generic.__value__, default, annotate_type_containers + ) + types: tuple[type, ...] origin = get_origin(type_generic) # For final we create the member corresponding to the inner type and set diff --git a/atom/typing_utils.py b/atom/typing_utils.py index 224372c5..1b92188a 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -6,6 +6,7 @@ # The full license is in the file LICENSE, distributed with this software. # -------------------------------------------------------------------------------------- import sys +import typing from itertools import chain from types import GenericAlias, UnionType from typing import ( @@ -23,6 +24,20 @@ get_origin, ) +try: + import typing_extensions +except ImportError: # pragma: no cover + typing_extensions = None + +_TYPE_ALIAS_TYPES = tuple( + alias_type + for alias_type in ( + getattr(typing, "TypeAliasType", None), + getattr(typing_extensions, "TypeAliasType", None), + ) + if alias_type is not None +) + if sys.version_info >= (3, 14): import annotationlib @@ -82,8 +97,23 @@ 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 _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 f00c94df..eac429f9 100644 --- a/tests/test_atom_from_annotations.py +++ b/tests/test_atom_from_annotations.py @@ -110,6 +110,21 @@ class B(A, use_annotations=True): assert B().a == 1 +def test_type_alias_in_annotation(): + try: + from typing_extensions import TypeAliasType + except ImportError: + pytest.skip("typing_extensions.TypeAliasType is unavailable") + + Alias = TypeAliasType("Alias", list[int]) + + class A(Atom, use_annotations=True, type_containers=1): + a: Alias + + assert isinstance(A.a, List) + assert isinstance(A.a.item, Int) + + def test_reject_bare_member_annotated_member(): with pytest.raises(ValueError) as e: From 8a045b75870b41025c95d5edcc9f3a10db158634 Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 13:58:26 +0100 Subject: [PATCH 2/4] update release notes --- releasenotes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/releasenotes.rst b/releasenotes.rst index 3d69f251..eb08d12d 100644 --- a/releasenotes.rst +++ b/releasenotes.rst @@ -1,6 +1,11 @@ Atom Release Notes ================== +0.14.0 - unreleased +------------------- + +- add support for type aliases in annotations used to generate a member PR #280 + 0.13.0 - 09/10/2026 ------------------- From e82988e23310d46d0a70962ffae49b10ee42d949 Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 14:00:38 +0100 Subject: [PATCH 3/4] fix type check --- atom/typing_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/typing_utils.py b/atom/typing_utils.py index 1b92188a..b5da29d3 100644 --- a/atom/typing_utils.py +++ b/atom/typing_utils.py @@ -27,7 +27,7 @@ try: import typing_extensions except ImportError: # pragma: no cover - typing_extensions = None + typing_extensions = None # type: ignore[assignment] _TYPE_ALIAS_TYPES = tuple( alias_type From d49f54a4ae6dcabb45df49ec8be7ab28422179ed Mon Sep 17 00:00:00 2001 From: mdartiailh Date: Tue, 3 Nov 2026 14:26:59 +0100 Subject: [PATCH 4/4] relax a bit mem leak tests to avoid spurious failures --- tests/test_mem.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_mem.py b/tests/test_mem.py index ab685e91..da8393ff 100644 --- a/tests/test_mem.py +++ b/tests/test_mem.py @@ -105,7 +105,8 @@ def test_mem_usage(label): last_info = stats.memory_info() # Allow slight memory decrease over time to make tests more resilient if first_info != last_info: - assert first_info.rss >= last_info.rss >= 0, ( + # Allow a small increase in RSS to account for memory fluctuations + assert (first_info.rss + 200e3) >= last_info.rss >= 0, ( "Memory leaked:\n {}\n {}".format(first_info, last_info) ) assert first_info.vms >= last_info.vms >= 0, (