pyanalyze.runtime

Expose an interface for a runtime type checker.

pyanalyze.runtime.is_compatible(value: object, typ: object) bool

Return whether value is compatible with type.

Examples:

>>> is_compatible(42, list[int])
False
>>> is_compatible([], list[int])
True
>>> is_compatible(["x"], list[int])
False
pyanalyze.runtime.get_compatibility_error(value: object, typ: object) str | None

Return an error message explaining why value is not compatible with type, or None if they are compatible.

Examples:

>>> print(get_compatibility_error(42, list[int]))
Cannot assign Literal[42] to list

>>> print(get_compatibility_error([], list[int]))
None
>>> print(get_compatibility_error(["x"], list[int]))
In element 0
  Cannot assign Literal['x'] to int