Fast Argument validation for functions using decorators
Project description
Validation Decorators
Simple and fast type-checking and parameter validation.
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, 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).resolver().is_file():
raise ValueError(...)
return True
class FileReader:
@validate_with(my_validation_func)
def __init__(self, file_path: 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
Close
Hashes for decorator_validation-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67ba2dff70a98d8d8c4c8d85f4525b4e9a0f24a21ffedf2b9e7948b34611bfca |
|
MD5 | 643d6e9b781924deb51ce4f9aee80b9d |
|
BLAKE2b-256 | cde0c69ad1ed996dc478f4fded012a99ad5d01aaa4f47b5fdf0ee19498cd2118 |
Close
Hashes for decorator_validation-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5504d4fd02c48411d8bc910247f340ce1f2f0500c485fdbbc9ce1f6502366963 |
|
MD5 | ea75ab7a4c7a2aa1f48db66eb765af7e |
|
BLAKE2b-256 | aee6135cc73077114294fd57d4071a4b11990e8062318aeae9a7b0a98d73d4d3 |