sevaht_utility.hinting¶
Runtime inspection of type hints and unions.
Helpers for working with annotations at runtime: flattening unions
(iterate_types()), checking a value against an expected runtime type
(verify_type()), and extracting a callable’s argument types
(get_callable_argument_hints()). These back the type-aware conversion in
sevaht_utility.parsing.csv_load().
- exception sevaht_utility.hinting.InvalidTypeError(value: object, *, expected_type: type[T] | UnionType)[source]¶
Bases:
TypeErrorRaised when a value’s type does not match the expected type.
- exception sevaht_utility.hinting.ParameterizedTypeNotSupportedError(expected_type: object)[source]¶
Bases:
TypeErrorRaised when verify_type receives a parameterized generic.
- sevaht_utility.hinting.iterate_types(*source_types: type | UnionType) Iterator[type][source]¶
Yield the distinct member types of one or more (possibly union) types.
Unions are flattened recursively (both
X | Yandtyping.Union), and duplicates are skipped, preserving first-seen order.- Parameters:
*source_types – Types or unions to flatten.
- Yields:
Each distinct constituent type, in order.
Example
>>> list(iterate_types(int | str)) [<class 'int'>, <class 'str'>]
- sevaht_utility.hinting.verify_type(expected_type: type | UnionType, value: T) T[source]¶
Return
valueif it matchesexpected_type, else raise.expected_typemay be a single type or a union of types;valuematches if it is an instance of any member (Anyandobjectalways match). Parameterized generics such aslist[int]cannot be checked at runtime and are rejected.- Parameters:
expected_type – The required type or union of types.
value – The value to check.
- Returns:
valueunchanged, for convenient inline use.- Raises:
InvalidTypeError –
valuematches no member ofexpected_type.ParameterizedTypeNotSupportedError –
expected_typecontains a parameterized generic.
- sevaht_utility.hinting.get_callable_argument_hints(function: Callable[..., Any]) dict[str, type][source]¶
Return a callable’s parameters mapped to their annotated types.
The
returnannotation is excluded, dataclassInitVarwrappers are unwrapped to their inner type, and unannotated parameters map toAny.- Parameters:
function – Any callable (function, dataclass type, etc.).
- Returns:
A mapping of parameter name to its type.