Fast Argument validation for functions using decorators
Project description
Validation Decorators
Simple and fast type-checking and parameter validation.
Install with
pip install decorator-validation
Why?
Do you want to remove visual clutter in your python function? Do you want to check types fast without a lot of boilerplate?
Then this package is right for you.
Not convinced? Check out the following:
# classic
def foo(bar: int, message: str, some_additional_info: dict):
if not isinstance(bar, int):
raise TypeError(...)
if not isinstance(message, str):
raise TypeError(...)
if not isinstance(some_additional_info, dict):
raise TypeError(...)
# now begin to code...
# now
@validate_types(bar=(int,), message=(str,), some_additional_info=(dict,))
def foo(bar: int, message: str, some_additional_info: dict):
# begin to code
If of course also supports multiple types:
from pathlib import Path
@validate_types(path=(str, Path), message=(str,))
def foo(path: str | Path, message: str):
# begin to code
Do you want to convert your input-types fast and without clutter?
def from_dict(dict_: dict):
return (dict_.get('bar'), dict_.get('message'), dict_.get('some_additional_info')), {}
@convert_with(from_dict)
def foo(bar: int, message:str, some_additional_info: dict):
...
Skip Type-checks by providing the SkipTypecheck
class as a type, this is very usefull for methods.
from decorator_validation.decorators import validate_types
from decorator_validation.types import SkipTypeCheck
class FileReader:
@validate_types((SkipTypeCheck,), file_path=(str,))
def __init__(self, file_path: str):
...
Of course, sometimes you want to have custom error messages. Then, just use the following code:
from decorator_validation.decorators import validate_with
from pathlib import Path
def my_validation_func(obj, file_path:str) -> True:
if not isinstance(file_path, str):
raise TypeError(...)
if not Path(file_path).resolve().is_file():
raise ValueError(...)
return True
class FileReader:
@validate_with(my_validation_func)
def __init__(self, file_path: str):
...
Map Multiple functions for subtypes
You can also directly map different validation functions to your arguments.
from decorator_validation.decorators import validate_map
from decorator_validation.std_validators import is_file
class FileReader:
@validate_map(None, file_path=is_file)
def __init__(self, file_path: str):
...
Validate Arbitrary Arguments
Starting with version 2.0.0
, the package allows for a single use decorator called validate
.
Depending on the argument, it will either check the type or use a function to validate. If a tuple is used, the standard typecheck will be applied, if not it expectes a Callable that returns a boolean value. All of the examples above can be directly copied and just the name of the decorator has to be changed.
An example.
from decorator_validation.decorators import validate
from decorator_validation.std_validators import is_file
class Logger:
@validate(file_path=is_file, message=(str,))
def log(file_path: str, message:str):
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for decorator_validation-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30a1cc43fae687ba60fbd4a40497ea12f2f101bf60c41e2da164aa9bee29ac49 |
|
MD5 | 102cc0029819aa6f98c58138a66c8bac |
|
BLAKE2b-256 | 078c99ae32307b49ef1c94a9a30d4520f7e85340f909062933a1f8c0b880caae |
Hashes for decorator_validation-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 188c00d6120a75f250f71f6c6f5bbbfee6a132741d109e65d41a1de94a6d67df |
|
MD5 | 537746f5d94efab49faca3bc59deba59 |
|
BLAKE2b-256 | 34434de7f4fe48302e27bd3b5997d24c3bfb597aec766fb81c4ee73e6a7f23e0 |