Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

pyvalid is a Python validation tool for checking of input function parameters and return values.

Project description

Python validation tool for checking function’s input parameters and return values. This module can be very helpful on the develop stage of the project, when control for accepted and returned function values is a one of most important things.

Module consists of two decorators: accepts and returns.

accepts(*accepted_arg_values, **accepted_kwargs_values)

A decorator for validating types and values of input parameters of a given function. You can pass the set of accepted types and values or validation function as decorator’s input parameters. Validation process can raise the following exceptions:

  • pyvalid.InvalidArgumentNumberError — when the number or position of arguments supplied to a function is incorrect.
  • pyvalid.ArgumentValidationError — when the type of an argument to a function is not what it should be.


A decorator for validating the return value of a given function. You can pass the set of accepted types and values or validation function as a decorator’s input parameters. Validation process can raise pyvalid.InvalidReturnType when the return value is not in the collection of supported values and types.

How to install

  • With PyPI: pip install -U pyvalid
  • Manually: python install

Example of usage

Function calc in example below has next limitations:

  • Can return only int or float value;
  • First parameter must be only of type str;
  • Second parameter must be of type int or equals to 2.0;
  • Third parameter must be of type int or float.
from pyvalid import accepts, returns

@returns(int, float)
@accepts(str, (int, 2.0), (int, float))
def calc(operator, val1, val2, val3):
    expression = '{v1} {op} {v2} {op} {v3}'.format(
        v1=val1, v2=val2, v3=val3
    return eval(expression)

# Output: 24.
print(calc('*', 2, 3, 4))

# Output: 24.0.
print(calc(operator='*', val1=2, val2=3.0, val3=4))

# Output: 24.0.
print(calc('*', 2.0, 3, 4))

# Raise pyvalid.ArgumentValidationError exception,
# because second argument has unsupported value.
print(calc('*', 3.14, 3, 4))

# Raise pyvalid.InvalidReturnType exception,
# because returns value is of type str.
print(calc('*', 2, 3, '"4"'))

Here is an example of usage pyvalid module in context of classes. Pay attention to the method connect of the class SqlDriver. This method is a good demonstration of usage accepts decorator for functions with keyword arguments.

from pyvalid import accepts, returns
from collections import Iterable

class SqlDriver(object):

    @accepts(object, host=str, port=int, usr=str, pwd=str, db=[str, None])
    def connect(self, **kwargs):
        connection_string = \
            'tsql -S {host} -p {port} -U {usr} -P {pwd} -D {db}'.format(**kwargs)
            print('Establishing connection: "{}"'.format(connection_string))
            # Create connection..
            success = True
            success = False
        return success

    def close(self):
            print('Closing connection')
            # Close connection..
            success = True
            success = False
        return success

    @returns(None, dict)
    @accepts(object, str, Iterable)
    def query(self, sql, params=None):
            query_info = 'Processing request "{}"'.format(sql)
            if params is not None:
                query_info += ' with following params: ' + ', '.join(params)
            # Process request..
            data = dict()
            data = None
        return data

sql_driver = SqlDriver()

conn_params = {
    'host': '',
    'port': 1433,
    'usr': 'admin',
    'pwd': 'Super_Mega_Strong_Password_2000',
    'db': 'info_tech'

sql = r'SELECT * FROM ProgrammingLang'
pl = sql_driver.query(sql)

sql = r'SELECT * FROM ProgrammingLang WHERE name=?'
python_pl = sql_driver.query(sql, ('Python',))


Following example with class User will show you how to use pyvalid module to validate some value with using validation function.

from pyvalid import accepts
from pyvalid.validators import is_validator

class User(object):

    class Validator(object):

        unsafe_passwords = [
            '111111', '000000', '123123',
            '123456', '12345678', '1234567890',
            'qwerty', 'sunshine', 'password',

        def login_checker(cls, login):
            is_valid = isinstance(login, str) and 1 <= len(login) <= 16
            if is_valid:
                for reg_user in User.registered:
                    if login == reg_user.login:
                        is_valid = False
            return is_valid

        def password_checker(cls, password):
            is_valid = isinstance(password, str) and \
                (6 <= len(password) <= 32) and \
                (password not in cls.unsafe_passwords)
            return is_valid

    registered = list()

    def __init__(self, login, password):
        self.__login = None
        self.login = login
        self.__password = None
        self.password = password

    def login(self):
        return self.__login

    @accepts(object, Validator.login_checker)
    def login(self, value):
        self.__login = value

    def password(self):
        return self.__password

    @accepts(object, Validator.password_checker)
    def password(self, value):
        self.__password = value

user = User('admin', 'Super_Mega_Strong_Password_2000')

# Output: admin Super_Mega_Strong_Password_2000
print(user.login, user.password)

# Raise pyvalid.ArgumentValidationError exception,
# because User.Validator.password_checker method
# returns False value.
user.password = 'qwerty'

# Raise pyvalid.ArgumentValidationError exception,
# because User.Validator.login_checker method
# returns False value.
user = User('admin', 'Super_Mega_Strong_Password_2001')


Note that this project is distributed under the MIT License.

Download files

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

Files for pyvalid, version 0.9.2
Filename, size File type Python version Upload date Hashes
Filename, size pyvalid-0.9.2-py3.6.egg (16.3 kB) File type Egg Python version 3.6 Upload date Hashes View hashes
Filename, size pyvalid-0.9.2-py3-none-any.whl (8.5 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size pyvalid-0.9.2.tar.gz (8.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page