mypythontools.types.validation module¶
Module with functions for ‘type_hints’ subpackage.
- exception mypythontools.types.validation.ValidationError[source]¶
Bases:
TypeError
To know that error is because of bad config type and not some TypeError from the inside.
- mypythontools.types.validation.small_validate(value, allowed_type: None | Any = None, name: str | None = None) None [source]¶
Type validation. It also works for Union and validate Literal values.
Instead of typeguard validation, it define just subset of types, but is simplier and needs no extra import, therefore can be faster.
- Parameters
value (Any) – Value that will be validated.
allowed_type (Any, optional) – For example int, str or list. It can be also Union or Literal. If Literal, validated value has to be one of Literal values. If None, it’s skipped. Defaults to None.
name (str | None, optional) – If error raised, name will be printed. Defaults to None.
- Raises
TypeError – Type does not fit.
Examples
>>> from typing_extensions import Literal ... >>> small_validate(1, int) >>> small_validate(None, Union[list, None]) >>> small_validate("two", Literal["one", "two"]) >>> small_validate("three", Literal["one", "two"]) Traceback (most recent call last): ValidationError: ...
- mypythontools.types.validation.typechecked_compatible(function)[source]¶
Turns off type checking for old incompatible python versions.
Mainly for new syntax like list[str] which raise TypeError.
- mypythontools.types.validation.validate_sequence(value, variable)[source]¶
Ensure, that defined sequence is not just a string.
Usually with Sequence we means for example tuple or list of items. String in some cases is not valid type then. This ensure, that this is Sequence, but not just string.
- Parameters
value (Sequence) – Variable where we want to ensure Sequence type.
variable (_type_) – If it is just string, this will raise an error with name of variable with incorrect type.
- Raises
TypeError – If it is just a string.