diff --git a/docs/api/typing_validation.validation_failure.rst b/docs/api/typing_validation.validation_failure.rst index 33fb694..3763a04 100644 --- a/docs/api/typing_validation.validation_failure.rst +++ b/docs/api/typing_validation.validation_failure.rst @@ -15,6 +15,13 @@ FailureTreeVisitor :show-inheritance: :members: +InvalidNumpyDTypeValidationFailure +---------------------------------- + +.. autoclass:: typing_validation.validation_failure.InvalidNumpyDTypeValidationFailure + :show-inheritance: + :members: + MissingKeysValidationFailure ---------------------------- diff --git a/docs/conf.py b/docs/conf.py index 5e424ff..f656fbd 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,9 +33,9 @@ # built documents. # # The full version, including alpha/beta/rc tags. -release = "1.2.4" +release = "1.2.9" # The short X.Y version. -version = "1.2.4" +version = "1.2.9" # -- General configuration --------------------------------------------------- diff --git a/typing_validation/__init__.py b/typing_validation/__init__.py index ea70364..0dea2f2 100644 --- a/typing_validation/__init__.py +++ b/typing_validation/__init__.py @@ -2,7 +2,7 @@ Runtime validation using type hints. """ -__version__ = "1.2.4" +__version__ = "1.2.9" from .inspector import TypeInspector, UnsupportedType from .validation import ( diff --git a/typing_validation/inspector.py b/typing_validation/inspector.py index 97ab058..28c4fac 100644 --- a/typing_validation/inspector.py +++ b/typing_validation/inspector.py @@ -332,13 +332,13 @@ def _repr( name = param.__name__ bound = param.__bound__ if bound is None: - lines = [indent+f"TypeVar({name!r})"] + lines = [indent + f"TypeVar({name!r})"] else: bound_lines, idx = self._repr(idx + 1, level + 1) lines = [ - indent+f"TypeVar({name!r}, bound=", + indent + f"TypeVar({name!r}, bound=", *bound_lines, - indent+")" + indent + ")", ] return lines, idx if tag == "union": @@ -423,9 +423,11 @@ def _repr( return lines, idx assert isinstance(param, int) lines = [ - indent + f"{pending_type.__name__}[" - if pending_type is not None - else indent + "Tuple[" + ( + indent + f"{pending_type.__name__}[" + if pending_type is not None + else indent + "Tuple[" + ) ] for _ in range(param): item_lines, idx = self._repr(idx + 1, level + 1) diff --git a/typing_validation/validation.py b/typing_validation/validation.py index e8b38d3..4f597b6 100644 --- a/typing_validation/validation.py +++ b/typing_validation/validation.py @@ -10,7 +10,15 @@ from keyword import iskeyword import sys import typing -from typing import Any, ForwardRef, Hashable, Optional, TypeVar, Union, get_type_hints +from typing import ( + Any, + ForwardRef, + Hashable, + Optional, + TypeVar, + Union, + get_type_hints, +) import typing_extensions from .validation_failure import ( @@ -220,6 +228,7 @@ def _type_error( setattr(error, "validation_failure", validation_failure) return error + def _typevar_error(val: Any, t: Any, bound_error: TypeError) -> TypeError: assert hasattr(bound_error, "validation_failure"), bound_error cause = getattr(bound_error, "validation_failure") @@ -229,6 +238,7 @@ def _typevar_error(val: Any, t: Any, bound_error: TypeError) -> TypeError: setattr(error, "validation_failure", validation_failure) return error + def _idx_type_error( val: Any, t: Any, idx_error: TypeError, *, idx: int, ordered: bool ) -> TypeError: @@ -524,6 +534,7 @@ def _extract_dtypes(t: Any) -> typing.Sequence[Any]: dtype for member in t.__args__ for dtype in _extract_dtypes(member) ] import numpy as np # pylint: disable = import-outside-toplevel + if hasattr(t, "__origin__"): t_origin = t.__origin__ if t_origin in { @@ -542,8 +553,10 @@ def _extract_dtypes(t: Any) -> typing.Sequence[Any]: return [t] raise TypeError() + def _validate_numpy_array(val: Any, t: Any) -> None: import numpy as np # pylint: disable = import-outside-toplevel + if not isinstance(val, TypeInspector): _validate_type(val, np.ndarray) assert hasattr(t, "__args__"), _missing_args_msg(t) @@ -588,6 +601,7 @@ def _validate_typevar(val: Any, t: TypeVar) -> None: except TypeError as e: raise _typevar_error(val, t, e) from None + # def _validate_callable(val: Any, t: Any) -> None: # """ # Callable validation diff --git a/typing_validation/validation_failure.py b/typing_validation/validation_failure.py index a4007a2..0263cac 100644 --- a/typing_validation/validation_failure.py +++ b/typing_validation/validation_failure.py @@ -197,9 +197,9 @@ def __repr__(self) -> str: def _str_type_descr(self, type_quals: tuple[str, ...] = ()) -> str: descr = ( - "type alias" if isinstance(self.t, str) - else "type variable" if isinstance(self.t, TypeVar) - else "type" + "type alias" + if isinstance(self.t, str) + else "type variable" if isinstance(self.t, TypeVar) else "type" ) if type_quals: descr = " ".join(type_quals) + " " + descr