vtypes (original) (raw)

vtypes - validating types

Python versions Build Status Tests Status codecov

Documentation PyPI Downloads Downloads per week GitHub stars

Validating types for python - use isinstance() to validate both type and value.

vtypes is a small library to define "validating types". These types can be used to add value validation on top of type checking anywhere where you usually rely on isinstance(). This can in particular be used to make validation schemas simpler and more readable, for example used in pyfields.

Installing

Usage

a - basics

A VType is a combination of :

A VType is constructed using vtype(name, type, validators, ...). For example we can create a positive int:

from vtypes import vtype PositiveInt = vtype('PositiveInt', int, {'should be positive': lambda x: x >= 0})

A VType's main purpose is to behave like a type (therefore to be compliant with isinstance) and to validate both type and values at the same time when isinstance is called:

assert isinstance(1, PositiveInt) assert not isinstance(-1, PositiveInt) # an int, but not >= 0

An alternate way to define a VType is to define a python class inheriting from VType.

So the following two classes are equivalent to our previous PositiveInt example:

`from vtypes import VType

type is provided in the ancestors

class PositiveInt(int, VType): validators = {'should be positive': lambda x: x >= 0}

type is provided in type

class PositiveInt2(VType): type = int validators = {'should be positive': lambda x: x >= 0} `

b - goodies

In addition to this primary feature, a VType provides a few handy methods:

`>>> PositiveInt.validate('size', -1)

ValidationError[ValueError]: Error validating [size=-1]. InvalidValue: should be positive. Function [] returned [False] for value -1. `

assert PositiveInt.has_valid_type(-1) # -1 is an int assert not PositiveInt.has_valid_value(-1) # -1 < 0

Finally, you may wish to use is_vtype to check if anything is a VType:

`from vtypes import is_vtype

assert is_vtype(PositiveInt) assert not is_vtype(int) assert not is_vtype(1) `

c - validators syntax

There are many ways to declare validators:

ConstrainedInt = vtype('ConstrainedInt', int, {'should be positive': lambda x: x >= 0, 'should be a multiple of 3': lambda x: x % 3})

Note that this syntax is valid8 simple syntax.

If you wish to create even more compact callables, you may wish to look at mini_lambda.

d - composition

You can combine types, for example a nonempty string can be obtained by mixing NonEmpty and str.

`NonEmpty = vtype('NonEmpty', (), {'should be non empty': lambda x: len(x) > 0}) """A VType describing non-empty containers, with strictly positive length."""

NonEmptyStr = vtype('NonEmptyStr', (NonEmpty, str), ()) """A VType for non-empty strings""" `

`class NonEmpty(VType): """A VType describing non-empty containers, with strictly positive length.""" validators = {'should be non empty': lambda x: len(x) > 0}

class NonEmptyStr(NonEmpty, str): """A VType for non-empty strings"""

class NonEmptyStr2(VType): """A VType for non-empty strings - alternate style""" type = NonEmpty, str `

All these behave as expected:

assert isinstance('hoho', NonEmptyStr) assert not isinstance('', NonEmptyStr) assert not isinstance(1, NonEmptyStr)

Main features

See Also

Do you like this library ? You might also like my other python libraries

Want to contribute ?

Details on the github page: https://github.com/smarie/python-vtypes