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: NotImplementedError

Raised 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 the int component is perfectly checkable.

This is a NotImplementedError rather than a validation failure: the value was never in question. Use can_validate to ask in advance whether a type can be honoured at all.

property explanation

Additional detail about what is unsupported, and what would fix it.

Return type:

str | None

property t

The type that cannot be validated against.

Return type:

Any

ValidationError

final class ValidationError(val, t, failure=None, /)[source]

Bases: TypeError

Raised when a value is not valid for a type.

This is a TypeError, so except TypeError catches 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.

property failure

The structured explanation of what went wrong, and where.

None when nothing built one — validated_iter reports the item it stopped at without diagnosing the whole iterable.

Return type:

ValidationFailure | None

property t

The type the value was validated against.

Return type:

Any

property val

The value that failed validation.

Return type:

Any