pycroscope.runtime¶
Expose an interface for a runtime type checker.
- pycroscope.runtime.is_assignable(value: object, typ: object) bool¶
Return whether
valueis assignable totyp.This is essentially a more powerful version of
isinstance(). Examples:>>> is_assignable(42, list[int]) False >>> is_assignable([], list[int]) True >>> is_assignable(["x"], list[int]) False
The term “assignable” is defined in the typing specification:
- pycroscope.runtime.get_assignability_error(value: object, typ: object) str | None¶
Return an error message explaining why
valueis not assignable totype, or None if it is assignable.Examples:
>>> print(get_assignability_error(42, list[int])) Cannot assign Literal[42] to list >>> print(get_assignability_error([], list[int])) None >>> print(get_assignability_error(["x"], list[int])) In element 0 Cannot assign Literal['x'] to int