From 2b510d667afe7f900c238f57fd0e0c71a54c74f2 Mon Sep 17 00:00:00 2001 From: Enrique Gonzalez Paredes Date: Tue, 9 Jan 2024 17:52:51 +0100 Subject: [PATCH] Fix bug in the special casing of `typing.Any` as a type. --- src/gt4py/eve/extended_typing.py | 8 ++++---- src/gt4py/eve/type_validation.py | 2 -- tests/eve_tests/unit_tests/test_type_validation.py | 5 +++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/gt4py/eve/extended_typing.py b/src/gt4py/eve/extended_typing.py index 17462a37ff..7bbf7c459b 100644 --- a/src/gt4py/eve/extended_typing.py +++ b/src/gt4py/eve/extended_typing.py @@ -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 ): @@ -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 diff --git a/src/gt4py/eve/type_validation.py b/src/gt4py/eve/type_validation.py index 2116969587..65f492ebfe 100644 --- a/src/gt4py/eve/type_validation.py +++ b/src/gt4py/eve/type_validation.py @@ -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)})." diff --git a/tests/eve_tests/unit_tests/test_type_validation.py b/tests/eve_tests/unit_tests/test_type_validation.py index 70ef033ff0..d9977f0d3a 100644 --- a/tests/eve_tests/unit_tests/test_type_validation.py +++ b/tests/eve_tests/unit_tests/test_type_validation.py @@ -28,6 +28,7 @@ ) from gt4py.eve.extended_typing import ( Any, + Callable, Dict, Final, ForwardRef, @@ -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):