check: The Verification Chamber#
- class my.typing.check.TypeCheck(*, data: T0 = None, root: TypeArg = None)#
Utilities for qualifying the types of data objects.
Exported as the static alias
tyc, and mixed intoTypist(soty.check,ty.is_atom, and friends resolve here). Each instance is an ephemeral pairing of one value with one target type; the classmethods construct and evaluate that pairing in a single call.Examples
Check values against (possibly nested) annotations:
>>> from my import tyc >>> tyc.check({'a': 1}, dict[str, int]) True >>> tyc.is_atom(3.5), tyc.is_vec([1]), tyc.is_map({}) (True, True, True)
I Instance State#
II Initial Methods#
III Public Methods#
- classmethod TypeCheck.is_stream(data: object) TypeIs[bytearray | memoryview | IO]#
Determine if a variable is a
Stream(a byte buffer or IO object).
- classmethod TypeCheck.is_string(data: object) TypeIs[str | bytes | bytearray | memoryview | IO]#
Determine if a variable is a
String(str, bytes, or another Stream).
- classmethod TypeCheck.is_scalar(data: object) TypeIs[int | float | complex | bool]#
Determine if a variable is a
Scalar(int, float, complex, or bool).
- classmethod TypeCheck.is_time(data: object) TypeIs[date | time | datetime | timedelta]#
Determine if a variable is a
Time(date, time, datetime, or timedelta).
- classmethod TypeCheck.is_atom(data: object) TypeIs[str | bytes | bytearray | memoryview | IO | int | float | complex | bool | date | time | datetime | timedelta | Enum]#
Determine if a variable is an
Atom(a String, Scalar, Time, or Enum).
- classmethod TypeCheck.is_vec(data: object) TypeIs[list | tuple | Set | deque | array | range]#
Determine if a variable is a
Vec(a list, tuple, set, or deque).
- classmethod TypeCheck.is_map(data: object) TypeIs[Mapping[Hashable, Any] | Iterable[tuple[Hashable, Any]] | ItemsView]#
Determine if a variable is a
Map(a Mapping or ItemsView).
- classmethod TypeCheck.is_map_item(data: object) bool#
Check if a value is a mapping item: a 2-tuple whose first element is hashable.
- Returns:
True if the value could serve as a (key, value) pair.
- classmethod TypeCheck.is_iter(data: object) TypeIs[Iterable | AsyncIterable]#
Determine if a variable is an
Iter: an iterable that is NOT of another known type.
- classmethod TypeCheck.is_struct(data: object) TypeIs[list | tuple | Set | deque | array | range | Mapping[Hashable, Any] | Iterable[tuple[Hashable, Any]] | ItemsView | BaseModel | object]#
Determine if a variable is a
Struct(a Vec, Map, or Model).
- classmethod TypeCheck.is_func(data: object) TypeIs[LambdaType | BuiltinMethodType]#
Determine if a variable is a
Func(any callable).
- classmethod TypeCheck.is_model(data: object) TypeIs[BaseModel | object]#
Determine if a variable is a
Model(a pydantic model, dataclass, or TypedDict).
- classmethod TypeCheck.is_literal(data: Any, tvar: MyType[TypeVar]) TypeGuard#
Check if a data matches this literal type.
- Parameters:
data – The data to check.
tvar – The literal type to check against.
- Returns:
True if data matches the literal members.
- classmethod TypeCheck.describe_func(fn: Callable[[Pp], Any]) tuple[dict[str, MyType], tuple[MyType, ...]]#
Extract the parameter and return-type annotations from a function’s signature.
- Parameters:
fn – The function to inspect.
- Returns:
A mapping of parameter names to their raw annotations.
A tuple of parsed return types (a union return is split into its members).
- classmethod TypeCheck.check(data: object, tvar: TypeArg) TypeIs#
Determine whether the given data is a valid instance of the given type.
- Parameters:
data – The data value to check. Ideally not an exhaustable iter.
tvar – The type to check against. Can be a raw type, a MyType, or a TypeArg.
- Returns:
True if all aspects of this type are satisfied by this data, including nested types.
Examples
Validate data against nested annotations:
>>> from my import tyc >>> tyc.check(5, int) True >>> tyc.check(['a', 1], list[str]) False >>> tyc.check(None, int | None) True
- classmethod TypeCheck.check_all(data: MapT, tvar: TypeArg[V]) TypeIs[MapT[Any, V]]#
- classmethod TypeCheck.check_all(data: VecT, tvar: TypeArg[V]) TypeIs[VecT[V]]
- classmethod TypeCheck.check_all(data: Iterable, tvar: TypeArg[V]) TypeIs[Iterable[V]]
Check if all values in an iterable match this type.
- Parameters:
data – The iterable of values to check.
tvar – The type to check each value against.
- Returns:
True if every value matches the type.