Code Monkey home page Code Monkey logo

pvv's Introduction

pvv

Minimal Python decorator to enforce type validation from type hints

Installation

This library has been created to have zero dependencies, and work with native Python. Use your favorite package manager to install pvv from PyPI.

pip install pvv

Usage

There are two ways to use the validate decorator from pvv:

Validate all parameters

Just use the decorator. A TypeError will be raised if the function is called with parameters of incorrect type.

Note

pvv will check if the given parameters to a function are instances of the class annotated as a parameter. If one parameter has no type hints, it will be ignored in the validation.

Warning

pvv will only work with type hints that can be used with class and instance checks.

from pvv import validate

@validate
def function(a: str, b: int, c):
    pass
>>> function("a", 3, 2)
>>> function(c=2, a="a", b=2)
>>> function(3, "a", 2)
TypeError: Incorrect type of function arguments: 'a' must be of type 'str', 'b' must be of type 'int'

Validate some parameters

from pvv import validate

@validate('a', 'c')
def function(a: str, b: int, c: bool | None):
    pass
>>> function("a", 3, True)
>>> function(3, "b", 0)
TypeError: Incorrect type of function arguments: 'a' must be of type 'str', 'c' must be of type 'bool | None'

An important note

The parameters of the validate decorator must all be of type str, otherwise a ValidatorError will be raised:

>>> @validate('a', 1)
... def func(*args, **kwargs):
...     pass
pvv.exceptions.ValidatorError: All arguments of decorator must be of type 'str'

pvv's People

Contributors

mianfg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.