Skip to content
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
13 changes: 12 additions & 1 deletion atom/meta/annotation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions atom/typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -23,6 +24,20 @@
get_origin,
)

try:
import typing_extensions
except ImportError: # pragma: no cover
typing_extensions = None # type: ignore[assignment]

_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

Expand Down Expand Up @@ -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."
Expand Down
5 changes: 5 additions & 0 deletions releasenotes.rst
Original file line number Diff line number Diff line change
@@ -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
-------------------

Expand Down
15 changes: 15 additions & 0 deletions tests/test_atom_from_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 2 additions & 1 deletion tests/test_mem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, (
Expand Down
Loading