torchoutil.pyoutil.typing.guards module¶
- is_builtin_number( ) typing_extensions.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.
- is_builtin_obj(
- x: Any,
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.
- is_builtin_scalar( ) typing_extensions.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 numbers. defaults to False.
- is_dataclass_instance(
- x: Any,
Returns True if argument is a dataclass.
Unlike function dataclasses.is_dataclass, this function returns False for a dataclass type.
- is_list_builtin_number(
- x: Any,
- is_namedtuple_instance(
- x: Any,
Returns True if argument is a NamedTuple.
- isinstance_guard( ) typing_extensions.TypeIs[T][source]¶
Improved isinstance(…) function that supports parametrized Union, TypedDict, Literal, Mapping or Iterable.
Example 1::¶
` >>> isinstance_guard({"a": 1, "b": 2}, dict[str, int]) # True >>> isinstance_guard({"a": 1, "b": 2}, dict[str, str]) # False >>> isinstance_guard({"a": 1, "b": 2}, dict) # True `