Skip to content

Commit

Permalink
fix | in sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
havogt committed Dec 3, 2024
1 parent 4aece8c commit f220b34
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/gt4py/eve/datamodels/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,11 +1041,6 @@ def _make_datamodel(
for key in annotations:
type_hint = annotations[key] = resolved_annotations[key]

if isinstance(
type_hint, types.UnionType
): # see https://github.com/python/cpython/issues/105499
type_hint = typing.Union[type_hint.__args__]

# Skip members annotated as class variables
if type_hint is ClassVar or xtyping.get_origin(type_hint) is ClassVar:
continue
Expand Down Expand Up @@ -1260,8 +1255,11 @@ def _make_concrete_with_cache(
if not is_generic_datamodel_class(datamodel_cls):
raise TypeError(f"'{datamodel_cls.__name__}' is not a generic model class.")
for t in type_args:
_accepted_types: tuple[type, ...] = (type, type(None), xtyping.StdGenericAliasType)
if sys.version_info >= (3, 10):
_accepted_types = (*_accepted_types, types.UnionType)
if not (
isinstance(t, (type, type(None), xtyping.StdGenericAliasType, types.UnionType))
isinstance(t, _accepted_types)
or (getattr(type(t), "__module__", None) in ("typing", "typing_extensions"))
):
raise TypeError(
Expand Down
9 changes: 9 additions & 0 deletions src/gt4py/eve/type_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import collections.abc
import dataclasses
import functools
import sys
import types
import typing

from . import exceptions, extended_typing as xtyping, utils
Expand Down Expand Up @@ -193,6 +195,12 @@ def __call__(
if type_annotation is None:
type_annotation = type(None)

if sys.version_info >= (3, 10):
if isinstance(
type_annotation, types.UnionType
): # see https://github.com/python/cpython/issues/105499
type_annotation = typing.Union[type_annotation.__args__]

# Non-generic types
if xtyping.is_actual_type(type_annotation):
assert not xtyping.get_args(type_annotation)
Expand Down Expand Up @@ -277,6 +285,7 @@ def __call__(

if issubclass(origin_type, (collections.abc.Sequence, collections.abc.Set)):
assert len(type_args) == 1
make_recursive(type_args[0])
if (member_validator := make_recursive(type_args[0])) is None:
raise exceptions.EveValueError(
f"{type_args[0]} type annotation is not supported."
Expand Down
1 change: 1 addition & 0 deletions tests/eve_tests/unit_tests/test_datamodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ class WrongModel:
("typing.Set[int]", ({1, 2, 3}, set()), (1, [1], (1,), {1: None})),
("typing.Union[int, float, str]", [1, 3.0, "one"], [[1], [], 1j]),
("int | float | str", [1, 3.0, "one"], [[1], [], 1j]),
("typing.List[int|float]", [[1, 2.0], []], [1, 2.0, [1, "2.0"]]),
("typing.Optional[int]", [1, None], [[1], [], 1j]),
(
"typing.Dict[Union[int, float, str], Union[Tuple[int, Optional[float]], Set[int]]]",
Expand Down

0 comments on commit f220b34

Please sign in to comment.