match: The Comparison Chamber#

class my.typing.match.TypeMatch#

Type matching utilities for MyType and Typist.

Exported as the static alias tym, and mixed into Typist (so ty.match and the is_*_type family resolve here). Matching compares types to types; see TypeCheck for comparing runtime values to types.

Examples

Compare generic types recursively, beyond what issubclass() can see:

>>> from collections.abc import Mapping
>>> from my import tym
>>> tym.match(dict[str, int], Mapping[str, int])
True
>>> tym.match(dict[str, int], Mapping[str, str])
False

I Primary Methods#

classmethod TypeMatch.is_map_item_type(tvar: TypeArg) → bool#

Check if this type represents a mapping item (2-tuple key-value pair).

II Public Methods#

classmethod TypeMatch.match(t0: None, t1: Any, intersect: bool = False) → False#
classmethod TypeMatch.match(t0: Any, t1: None, intersect: bool = False) → False
classmethod TypeMatch.match(t0: type, t1: TypeArg[T1], intersect: bool = False) → TypeIs[type[T1]]
classmethod TypeMatch.match(t0: MyType, t1: TypeArg[T1], intersect: bool = False) → TypeIs[MyType[T1]]
classmethod TypeMatch.match(t0: tuple[type, ...], t1: TypeArg[T1], intersect: bool = False) → TypeIs[tuple[type, ...]]

Check if the first type is valid subset of the second (or intersects, if so set).

Parameters:
  • t0 – The source type.

  • t1 – The target type.

  • intersect – If True, check for any overlap between the two types rather than full subset coverage.

Examples

Subset matching is directional; intersect loosens it to any overlap:

>>> from my import tym
>>> tym.match(int, int | str)
True
>>> tym.match(int | str, int)
False
>>> tym.match(int | str, int, intersect=True)
True
classmethod TypeMatch.is_stream_type(tvar: MyType) → TypeIs[MyType[bytearray | memoryview | IO]]#
classmethod TypeMatch.is_stream_type(tvar: type) → TypeIs[type[bytearray | memoryview | IO]]

Determine if the given type is a Stream type.

classmethod TypeMatch.is_string_type(tvar: MyType) → TypeIs[MyType[str | bytes | bytearray | memoryview | IO]]#
classmethod TypeMatch.is_string_type(tvar: type) → TypeIs[type[str | bytes | bytearray | memoryview | IO]]

Determine if the given type is a String type.

classmethod TypeMatch.is_scalar_type(tvar: MyType) → TypeIs[MyType[int | float | complex | bool]]#
classmethod TypeMatch.is_scalar_type(tvar: type) → TypeIs[type[int | float | complex | bool]]

Determine if the given type is a Scalar type.

classmethod TypeMatch.is_time_type(tvar: MyType) → TypeIs[MyType[date | time | datetime | timedelta]]#
classmethod TypeMatch.is_time_type(tvar: type) → TypeIs[type[date | time | datetime | timedelta]]

Determine if the given type is a Time type.

classmethod TypeMatch.is_atom_type(tvar: MyType) → TypeIs[MyType[str | bytes | bytearray | memoryview | IO | int | float | complex | bool | date | time | datetime | timedelta | Enum]]#
classmethod TypeMatch.is_atom_type(tvar: type) → TypeIs[type[str | bytes | bytearray | memoryview | IO | int | float | complex | bool | date | time | datetime | timedelta | Enum]]

Determine if the given type is an Atom type.

classmethod TypeMatch.is_vec_type(tvar: MyType) → TypeIs[MyType[list | tuple | Set | deque | array | range]]#
classmethod TypeMatch.is_vec_type(tvar: type) → TypeIs[type[list | tuple | Set | deque | array | range]]

Determine if the given type is a Vec type.

classmethod TypeMatch.is_map_type(tvar: MyType) → TypeIs[MyType[Mapping | ItemsView]]#
classmethod TypeMatch.is_map_type(tvar: type) → TypeIs[type[Mapping | ItemsView]]

Determine if a type is a Map: a Mapping/ItemsView, or an iterable of pairs.

classmethod TypeMatch.is_iter_type(tvar: MyType) → TypeIs[MyType[Iterable | AsyncIterable]]#
classmethod TypeMatch.is_iter_type(tvar: type) → TypeIs[type[Iterable | AsyncIterable]]

Determine if the given type is a non-struct, non-atomic Iterable (mostly iterators).

classmethod TypeMatch.is_struct_type(tvar: MyType) → TypeIs[MyType[list | tuple | Set | deque | array | range | Mapping | ItemsView | BaseModel | object]]#
classmethod TypeMatch.is_struct_type(tvar: type) → TypeIs[type[list | tuple | Set | deque | array | range | Mapping | ItemsView | BaseModel | object]]

Determine if the given type is a Struct type (any Iterable or Model).

classmethod TypeMatch.is_func_type(tvar: MyType) → TypeIs[MyType[function | builtin_function_or_method]]#
classmethod TypeMatch.is_func_type(tvar: type) → TypeIs[type[LambdaType | BuiltinMethodType]]

Determine if the given type is a Func type.

classmethod TypeMatch.is_model_type(tvar: MyType) → TypeIs[MyType[M]]#
classmethod TypeMatch.is_model_type(tvar: type) → TypeIs[type[M]]

Determine if the given type is a Model type (pydantic model, dataclass, or TypedDict).