pythonwrench.typing.checks module¶
- pythonwrench.typing.checks.check_args_types(fn: Callable[[P], T]) Callable[[P], T][source]¶
Decorator to check argument types before call to a function.
Example¶
>>> import pythonwrench as pw >>> @pw.check_args_types >>> def f(a: int, b: str) -> str: >>> return a * b >>> f(1, "a") # pass check >>> f(1, 2) # raises TypeError from decorator
-
pythonwrench.typing.checks.is_builtin_collection(x: Any, *, strict: bool =
False) TypeIs[list | tuple | dict | set | frozenset][source]¶ Returns True if x is an instance of a builtin collection type (list, tuple, dict, set, frozenset).
- Args:
x: Object to check. strict: If True, it will not consider custom subtypes of builtins as builtin collections. defaults to False.
-
pythonwrench.typing.checks.is_builtin_number(x: Any, *, strict: bool =
False) TypeIs[bool | int | float | complex][source]¶ Returns True if x is an instance of a builtin number type (int, float, bool, complex).
- Args:
x: Object to check. strict: If True, it will not consider custom subtypes of builtins as builtin numbers. defaults to False.
- pythonwrench.typing.checks.is_builtin_obj(x: Any) bool[source]¶
Returns True if object is an instance of a builtin object.
Note: If the object is an instance of a custom subtype of a builtin object, this function returns False.
-
pythonwrench.typing.checks.is_builtin_scalar(x: Any, *, strict: bool =
False) TypeIs[bool | int | float | complex | None | str | bytes][source]¶ Returns True if x is an instance of a builtin scalar type (int, float, bool, complex, NoneType, str, bytes).
- Args:
x: Object to check. strict: If True, it will not consider subtypes of builtins as builtin scalars. defaults to False.
- pythonwrench.typing.checks.is_dataclass_instance(x: Any) TypeIs[DataclassInstance][source]¶
Returns True if argument is a dataclass.
Unlike function dataclasses.is_dataclass, this function returns False for a dataclass type.
-
pythonwrench.typing.checks.is_iterable_bool(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[bool]][source]¶
-
pythonwrench.typing.checks.is_iterable_bytes_or_list(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[bytes | list]][source]¶
-
pythonwrench.typing.checks.is_iterable_float(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[float]][source]¶
-
pythonwrench.typing.checks.is_iterable_int(x: Any, *, accept_bool: bool =
True, accept_generator: bool =True) TypeIs[Iterable[int]][source]¶
-
pythonwrench.typing.checks.is_iterable_integral(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[Integral]][source]¶
-
pythonwrench.typing.checks.is_iterable_str(x: Any, *, accept_str: bool =
True, accept_generator: bool =True) TypeGuard[Iterable[str]][source]¶
- pythonwrench.typing.checks.is_namedtuple_instance(x: Any) TypeIs[NamedTupleInstance][source]¶
Returns True if argument is a NamedTuple.
-
pythonwrench.typing.checks.is_sequence_str(x: Any, *, accept_str: bool =
True) TypeGuard[Sequence[str]][source]¶
-
pythonwrench.typing.checks.isinstance_generic(obj: Any, class_or_tuple: type[T] | None | tuple[type[T], ...] | Any, *, check_only_first: bool =
False) TypeIs[T][source]¶ Improved isinstance(…) function that supports parametrized Union, TypedDict, Literal, Mapping or Iterable.
- Args:
obj: Object to check. class_or_tuple: Type to check. Can be a parametrized type from typing. check_only_first: If True, check only if first element when checking for Iterable[type]. defaults to False.
Example 1¶
>>> isinstance_generic({"a": 1, "b": 2}, dict) ... True >>> isinstance_generic({"a": 1, "b": 2}, dict[str, int]) ... True >>> isinstance_generic({"a": 1, "b": 2}, dict[str, str]) ... False >>> from typing import Literal >>> isinstance_generic({"a": 1, "b": 2}, dict[str, Literal[1, 2]]) ... True