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
@check_types()
def foo(bar: int, message: str, some_additional_info: dict):
# begin to code
Checkout the codebase for more examples and built in decorators!
NOTE:
check_types
has limitations for python versions lower than 3.10 due to lack of built in language support for type-checking. Use with caution with special types and None-Types!
More Examples
Note:
check_types
is only available since release 2.2.0. for previous releases checkoutvalidate_types
. For more details check out the repository and older releases :).
If of course also supports multiple types:
from pathlib import Path
@check_types()
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 enables you
to skip the type check even if you have type-annotations.
This is usefull if you do not wont to check some stuff but want to use the type-hints
from decorator_validation.decorators import validate_types
from decorator_validation.types import SkipTypeCheck
class FileReader:
@check_types(self=(SkipTypeCheck,), file_path=(str,))
def __init__(self, file_path: str):
...
Of course, sometimes you want to have a custom validation method for all your inputs.
Then, just use the validate_with
function.
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.
A Validation function is a function that returns True
of False
(or raises an exception) if the input is correct / incorrect.
from decorator_validation.decorators import check_types
from decorator_validation.std_validators import is_file
class FileReader:
@check_types(file_path=is_file)
def __init__(self, file_path: str):
...
Validate Arbitrary Arguments
You can of course combine validation functions with type-check-skipping and the signature_usage.
from decorator_validation.decorators import check_types
from decorator_validation.types import SkipTypeCheck
from decorator_validation.std_validators import is_file
class Logger:
@check_types(file_path=is_file, message=(SkipTypeCheck,))
def log(file_path: str, message: str, repeat: int = 1):
...
log('some_file.txt', 'hello') # works
log('some_file.txt', 2) # works
log('some_file.txt', 2, '3') # does not work
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-3.0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | db70a2b2148076bdb450d045523a90ddc5fc809bca2b794c3fa88f26c86a1e87 |
|
MD5 | df7762a172f9362b3100c77624abad8d |
|
BLAKE2b-256 | b1528b07c5c339f869c454e85cee6b2ccfbd604555b13d88919213b4c2412cdc |
Hashes for decorator_validation-3.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d80d97c233573fb1b9395dfd36aac1cfa8eabd5d9bb59eb6a2872c2d6c033b5 |
|
MD5 | fcbe65fa2a89e101c1672c55a1c3b584 |
|
BLAKE2b-256 | 8a63c13b8851adfe04ea1821de11a0a8af7ad2d175dd59edad89826510a67311 |