Skip to main content

Fast Argument validation for functions using decorators

Project description

Validation Decorators

Unittest Static Badge PyPI - Python Version Poetry

Simple and fast type-checking and parameter validation at runtime.

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
from decorator_validation import check_types

@check_types
def foo(bar: int, message: str, some_additional_info: dict):
    # begin to code

The check_types decorator takes override kwargs to overwrite the type-hints

@check_types(bar=float, message=(str, bytes))
def foo(bar: int, message: str, some_additional_info: dict):
    # begin to code

although this is more usefull to skip a check with

from decorator_validation import check_types, SkipTypeCheck

@check_types(message=SkipTypeCheck)
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! Furthermore wrap types inside of tuples if they are not of type type!

More Example

Of course, sometimes you want to have a custom validation method for all your inputs. It just needs to return if the input is valid or not. You can use the make_validator function to shorten the code even more.

from decorator_validation import check_types, make_validator
from pathlib import Path

@make_validator
def my_validation_func(file_path: str) -> True:
    if not isinstance(file_path, str):
        raise TypeError(...)
    if not Path(file_path).resolve().is_file():
        raise ValueError(...)

class FileReader:

    @check_types(file_path=my_validation_func)
    def __init__(self, file_path: str):
        ...

Default Validators

This Repository comes with its own default validation functions, like if an input is a valid file. A Validation function is a function that returns True of False (or raises an exception) if the input is correct / incorrect.

from decorator_validation 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. SkipTypeCheck can be used for non-supported types in type-hinting etc.

from decorator_validation import check_types, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

decorator_validation-4.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

decorator_validation-4.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file decorator_validation-4.1.0.tar.gz.

File metadata

  • Download URL: decorator_validation-4.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.2 Windows/10

File hashes

Hashes for decorator_validation-4.1.0.tar.gz
Algorithm Hash digest
SHA256 d0928cbbe770ba07233d97da5de9eb1340dc90ba54f958cc3e825f71dbc285c3
MD5 7c2f6af65ad09641793b7f4965a3e8b9
BLAKE2b-256 310aeaf5f250357f98b903fa74ff62ee8e90f61c0e7520fcc03dcefaa957f731

See more details on using hashes here.

File details

Details for the file decorator_validation-4.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for decorator_validation-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7f2d79dc72778b34900ddc4ca19e0a5547bebeaa92cc7c024d7acee07253d53
MD5 876218f0b2fb266bbb47007a6de2013f
BLAKE2b-256 7a91f345d6ec295e5baf6256fc7dcc070de55604e63829b5cab23eca944dcd37

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page