pyanalyze.runtime¶
Expose an interface for a runtime type checker.
- pyanalyze.runtime.is_assignable(value: object, typ: object) bool ¶
Return whether
value
is 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:
- pyanalyze.runtime.get_assignability_error(value: object, typ: object) str | None ¶
Return an error message explaining why
value
is 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
- pyanalyze.runtime.is_compatible(value: object, typ: object) bool ¶
Deprecated alias for is_assignable(). Use that instead.
- pyanalyze.runtime.get_compatibility_error(value: object, typ: object) str | None ¶
Deprecated alias for get_assignability_error(). Use that instead.