Skip to content

Commit

Permalink
Fix bug in the special casing of typing.Any as a type.
Browse files Browse the repository at this point in the history
  • Loading branch information
egparedes committed Jan 9, 2024
1 parent da6507b commit 2b510d6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/gt4py/eve/extended_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def _patched_proto_hook(other): # type: ignore[no-untyped-def]
if isinstance(_typing.Any, type): # Python >= 3.11
_ArtefactTypes = (*_ArtefactTypes, _typing.Any)

# `Any` is a class since typing_extensions >= 4.4
# `Any` is a class since typing_extensions >= 4.4 and Python 3.11
if (typing_exts_any := getattr(_typing_extensions, "Any", None)) is not _typing.Any and isinstance(
typing_exts_any, type
):
Expand All @@ -504,11 +504,11 @@ def is_actual_type(obj: Any) -> TypeGuard[Type]:
"""Check if an object has an actual type and instead of a typing artefact like ``GenericAlias`` or ``Any``.
This is needed because since Python 3.9:
``isinstance(types.GenericAlias(), type) is True``
``isinstance(types.GenericAlias(), type) is True``
and since Python 3.11:
``isinstance(typing.Any, type) is True``
``isinstance(typing.Any, type) is True``
"""
return isinstance(obj, type) and type(obj) not in _ArtefactTypes
return isinstance(obj, type) and obj not in _ArtefactTypes


if hasattr(_typing_extensions, "Any") and _typing.Any is not _typing_extensions.Any: # type: ignore[attr-defined] # _typing_extensions.Any only from >= 4.4
Expand Down
2 changes: 0 additions & 2 deletions src/gt4py/eve/type_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,6 @@ def make_is_instance_of(name: str, type_: type) -> FixedTypeValidator:
"""Create an ``FixedTypeValidator`` validator for a specific type."""

def _is_instance_of(value: Any, **kwargs: Any) -> None:
if type_ is Any:
return
if not isinstance(value, type_):
raise TypeError(
f"'{name}' must be {type_} (got '{value}' which is a {type(value)})."
Expand Down
5 changes: 3 additions & 2 deletions tests/eve_tests/unit_tests/test_type_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from gt4py.eve.extended_typing import (
Any,
Callable,
Dict,
Final,
ForwardRef,
Expand All @@ -41,8 +42,8 @@
)


VALIDATORS: Final = [type_val.simple_type_validator]
FACTORIES: Final = [type_val.simple_type_validator_factory]
VALIDATORS: Final[list[Callable]] = [type_val.simple_type_validator]
FACTORIES: Final[list[Callable]] = [type_val.simple_type_validator_factory]


class SampleEnum(enum.Enum):
Expand Down

0 comments on commit 2b510d6

Please sign in to comment.