typing_validation.errors
Errors raised by validation.
The two errors answer different questions — “I cannot check this” versus
“I checked this and it is wrong” — and they deliberately share no base beyond
Exception. A caller must be able to tell them apart, and a test must
never be able to mistake one for the other.
TYPE_CHECKING
- TYPE_CHECKING = False
Returns True when the argument is true, False otherwise. The builtins True and False are the only two instances of the class bool. The class bool is a subclass of the class int, and cannot be subclassed.
UnsupportedTypeError
- final class UnsupportedTypeError(t, explanation=None, /)[source]
Bases:
NotImplementedErrorRaised when a type is not one that this library knows how to validate against.
Support is all-or-nothing: an unsupported component makes the whole type unsupported, so this is raised for
tuple[int, Callable[[int], int]]even though theintcomponent is perfectly checkable.This is a
NotImplementedErrorrather than a validation failure: the value was never in question. Usecan_validateto ask in advance whether a type can be honoured at all.- property explanation
Additional detail about what is unsupported, and what would fix it.
ValidationError
- final class ValidationError(val, t, failure=None, /)[source]
Bases:
TypeErrorRaised when a value is not valid for a type.
This is a
TypeError, soexcept TypeErrorcatches it. It is a distinct subclass so that you can tell “this library rejected the value” apart from “something raised a TypeError” — catch this rather than the base class if you mean the former.The structured explanation of what went wrong, and where, is on
failure.