pythonwrench.typing package¶
- pythonwrench.typing.EllipsisType¶
alias of
EllipsisType
- class pythonwrench.typing.SupportsAdd(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Other]Protocol that support __add__ (+) method.
- class pythonwrench.typing.SupportsAnd(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Other]Protocol that support __and__ (&) method.
- class pythonwrench.typing.SupportsBool(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol that support __bool__ method.
- class pythonwrench.typing.SupportsDiv(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Other]Protocol that support __div__ (/) method.
- class pythonwrench.typing.SupportsGetitem(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Item,_T_Index]Protocol that support __getitem__ method.
- class pythonwrench.typing.SupportsGetitem2(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Index2,_T_Item]Protocol that support __getitem__ method.
Same than SupportsGetitem except that generic parameters are in reversed order: [T_Index, T_Item].
- class pythonwrench.typing.SupportsGetitemIterLen(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Item,_T_Index]Protocol that support __getitem__, __iter__ and __len__ methods.
- class pythonwrench.typing.SupportsGetitemIterLen2(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Index2,_T_Item]Protocol that support __getitem__, __iter__ and __len__ methods.
Same than SupportsGetitemIterLen except that generic parameters are in reversed order: [T_Index, T_Item].
- class pythonwrench.typing.SupportsGetitemLen(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Item,_T_Index]Protocol that support __getitem__ and __len__ methods.
- class pythonwrench.typing.SupportsGetitemLen2(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Index2,_T_Item]Protocol that support __getitem__ and __len__ methods.
Same than SupportsGetitemLen except that generic parameters are in reversed order: [T_Index, T_Item].
- class pythonwrench.typing.SupportsIterLen(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Item]Protocol that support __iter__ and __len__ methods.
- class pythonwrench.typing.SupportsLen(*args, **kwargs)[source]¶
Bases:
ProtocolProtocol that support __len__ method.
- class pythonwrench.typing.SupportsMatmul(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Other]Protocol that support __matmul__ (@) method.
- class pythonwrench.typing.SupportsMul(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Other]Protocol that support __mul__ (*) method.
- class pythonwrench.typing.SupportsOr(*args, **kwargs)[source]¶
Bases:
Protocol[_T_Other]Protocol that support __or__ (|) method.
- pythonwrench.typing.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.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.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.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.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.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.is_iterable_bool(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[bool]][source]¶
-
pythonwrench.typing.is_iterable_bytes_or_list(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[bytes | list]][source]¶
-
pythonwrench.typing.is_iterable_float(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[float]][source]¶
-
pythonwrench.typing.is_iterable_int(x: Any, *, accept_bool: bool =
True, accept_generator: bool =True) TypeIs[Iterable[int]][source]¶
-
pythonwrench.typing.is_iterable_integral(x: Any, *, accept_generator: bool =
True) TypeIs[Iterable[Integral]][source]¶
-
pythonwrench.typing.is_iterable_str(x: Any, *, accept_str: bool =
True, accept_generator: bool =True) TypeGuard[Iterable[str]][source]¶
- pythonwrench.typing.is_namedtuple_instance(x: Any) TypeIs[NamedTupleInstance][source]¶
Returns True if argument is a NamedTuple.
-
pythonwrench.typing.is_sequence_str(x: Any, *, accept_str: bool =
True) TypeGuard[Sequence[str]][source]¶
-
pythonwrench.typing.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
Submodules¶
- pythonwrench.typing.checks module
- pythonwrench.typing.checks.check_args_types
- pythonwrench.typing.checks.is_builtin_collection
- pythonwrench.typing.checks.is_builtin_number
- pythonwrench.typing.checks.is_builtin_obj
- pythonwrench.typing.checks.is_builtin_scalar
- pythonwrench.typing.checks.is_dataclass_instance
- pythonwrench.typing.checks.is_iterable_bool
- pythonwrench.typing.checks.is_iterable_bytes_or_list
- pythonwrench.typing.checks.is_iterable_float
- pythonwrench.typing.checks.is_iterable_int
- pythonwrench.typing.checks.is_iterable_integral
- pythonwrench.typing.checks.is_iterable_str
- pythonwrench.typing.checks.is_namedtuple_instance
- pythonwrench.typing.checks.is_sequence_str
- pythonwrench.typing.checks.is_typed_dict
- pythonwrench.typing.checks.isinstance_generic
- pythonwrench.typing.classes module
- pythonwrench.typing.classes.DataclassInstance
- pythonwrench.typing.classes.NamedTupleInstance
- pythonwrench.typing.classes.SupportsAdd
- pythonwrench.typing.classes.SupportsAnd
- pythonwrench.typing.classes.SupportsBool
- pythonwrench.typing.classes.SupportsDiv
- pythonwrench.typing.classes.SupportsGetitem
- pythonwrench.typing.classes.SupportsGetitem2
- pythonwrench.typing.classes.SupportsGetitemIterLen
- pythonwrench.typing.classes.SupportsGetitemIterLen2
- pythonwrench.typing.classes.SupportsGetitemLen
- pythonwrench.typing.classes.SupportsGetitemLen2
- pythonwrench.typing.classes.SupportsIterLen
- pythonwrench.typing.classes.SupportsLen
- pythonwrench.typing.classes.SupportsMatmul
- pythonwrench.typing.classes.SupportsMul
- pythonwrench.typing.classes.SupportsOr