Skip to main content

Custom argument validation, injected directly in the function signature.

Project description

Test Package version Supported Python versions

Validargs

  1. Description
  2. Requirements
  3. Installation
  4. Usage
  5. Contributions

Description

A function decorator that is using the inspect library to validate arguments passed in the decorated function.

It allows writing complex and re-usable argument validation rules injected directly in the function signature.

The validation is not based on type annotations, which allows to go beyond the basic type validations.

NOTE: pydantic is a far more advanced and very well supported library, but requires to model your arguments. If you haven't already, make sure you check it out.

Source Code: https://github.com/kolitiri/validargs

Requirements

Python 3.8+

Installation

pip install validargs

Usage

For best results, use the argument definitions syntax as of python v3.8.

Use "/" and "*" to distinguish between "positional only", "positional or keyword" and "keyword only" arguments.

Define custom validation rules using the Validator class.

""" my_app.py """
from typing import Any

from validargs import validated, Validator


def positive_number(argument: int) -> None:
    if argument is None:
        return None
    if type(argument) is not int:
        raise TypeError("Number must be an integer")
    if argument <= 0:
        raise Exception("Number must be a positive integer")


def short_str(argument: str) -> None:
    if argument is None:
        return None
    if len(argument) > 20:
        raise Exception("String too long")

Use the validated decorator to decorate the functions that you want validated.

@validated
def my_function(
    positive_number: int = Validator(positive_number),
    /,
    short_string: str = Validator(short_str),
    *,
    another_short_string: str = Validator(short_str),
):
    pass

You can mix and match arguments with and without validators and also assign default values.

@validated
def my_function(
    positive_number: int = Validator(positive_number, default_value=10), # Validator with default value
    non_validated_arg: int = 0, # Regular argument with default value
    /,
    short_string: str = Validator(short_str), # Validator without default value
    *,
    another_validated_argument: str, # Regular argument without default value
):
    pass

Contributions

If you want to contribute to the package, please have a look at the CONTRIBUTING.md file for some basic instructions. Feel free to reach me in my email or my twitter account, which you can find in my github profile!

License

This project is licensed under the terms of the MIT license.

Authors

Christos Liontos

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

validargs-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

validargs-0.1.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file validargs-0.1.1.tar.gz.

File metadata

  • Download URL: validargs-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.0 Darwin/21.6.0

File hashes

Hashes for validargs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f2609624fda0e72075d93383c4e9457c21f44b4577962ebbd1f19d4ab1fada2d
MD5 35791236eb1e6a1467500d2196b78d42
BLAKE2b-256 432ad85517f7af343ef8a0a92d11f8ab6222fabb747397d8da95fb7418f14c3e

See more details on using hashes here.

File details

Details for the file validargs-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: validargs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0 CPython/3.10.0 Darwin/21.6.0

File hashes

Hashes for validargs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1bddd147bed301a70ec93b993505a33c12799d8074efec2b3ef8973499de5bc2
MD5 3c428877edbbcb7ef47feb3c99803f9b
BLAKE2b-256 669d0e020c505e23ffa8925a0241162045c3c348118095a7282199dfc8dd89cd

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