A dynamic runtime typing validation for members annotated with PEP 484 type hints.
TangoMan PyValidator idea was inspired by agronholm / typeguard but enforces SOLID principles, and is based on the chain-of-responsibility design pattern which makes it more readable, extensible and maintenable.
TangoMan PyValidator provides @Validator.type_check decorator as a mean to dynamically check for type violations at runtime.
Enter following command to install locally
$ sudo python3 setup.py install
# or
$ pip3 install git+https://github.com/TangoMan75/pyvalidator- Validate typing aliases recursively.
- Validate standard python types.
- Validate classes.
- Validate nullable properties.
- Validate not blank properties.
Python utilities to handle reflexion methods on functions.
Import typing module and extend your class with Validator
import typing
from pyvalidator.validator import Validator
class Foobar(Validator):
# ...Document your class with appropriate annotations and use @Validator.type_check decorator on your method and properties.
@property
def nullable_str(self) -> typing.Optional[str]:
return self._nullable_str
@nullable_str.setter
@Validator.type_check
def nullable_str(self, nullable_str_: typing.Optional[str]) -> None:
self._nullable_str = nullable_str_At runtime if any given argument does not match expected type, Validator will raise a TypeError.
Use @Validator.not_blank decorator on your method and properties.
@Validator.not_blank
def divide(self, a: int, b: int) -> None:
return a / bAt runtime Validator will raise a ValueError if any given argument is empty i.e:
bytesequalsb''bytearrayequalsbytearray()complexequals0jdictequals{}floatequals0.0frozensetequalsfrozenset()intequals0listequals[]memoryviewequalsmemoryview(b'')rangeequalsrange(0)setequalsset()strequals''tupleequals()
NOTE: "bool" and "type" values excluded, since "not blank" does not make sense with them.
Decorators are chainable, you can use @Validator.type_check and @Validator.not_blank on the same attribute.
@property
def email(self) -> typing.Optional[str]:
return self._email
@email.setter
@Validator.type_check
@Validator.not_blank
def email(self, email_: typing.Optional[str]) -> None:
self._email = email_typing.OrderedDict causes AttributeError at runtime (which causes travis ci to fail):
AttributeError: module 'typing' has no attribute 'OrderedDict'
Seems like mypy maintainers don't want to fix the issue python/mypy#6904
If you find any bug please report here : Issues
Copyrights (c) 2020 "Matthias Morin" <[email protected]>
Distributed under the MIT license.
If you like TangoMan PyValidator please star, follow or tweet:
... And check my other cool projects.