Skip to main content

Library for creating utility classes giving a nice abstraction for type checking and data validation

Project description

CheckTypes package

Library for creating utility classes giving a nice abstraction for type checking and data validation.

Basic examples

Creation

Object Oriented API

>>> from checktypes import CheckType
>>> class PositiveInt(int, CheckType):
...     "'int' > 0"
...     @staticmethod
...     def predicate(n):
...         return n > 0
...
>>> class HumanAge(int, CheckType):
...     "'int' between 0 and 125"
...     minval = 0
...     maxval = 125
...     @classmethod
...     def predicate(cls, val):
...         return cls.minval <= val <= cls.maxval
...

You can also use lambdas:

>>> class PositiveInt(int, CheckType):
...     "'int' > 0"
...     predicate = lambda n: n > 0
...     # If your linter doesn't like it, try:
...     # predicate = staticmethod(lambda n: n > 0)
...
>>> class HumanAge(int, CheckType):
...     "'int' between 0 and 125"
...     minval = 0
...     maxval = 125
...     predicate = classmethod(lambda cls, n: cls.minval <= n <= cls.maxval)
...

Functional API

>>> from checktypes import checktype
>>> PositiveInt = checktype('PositiveInt', int, lambda n: n > 0, "'int' > 0")
>>> HumanAge = checktype(
...     'HumanAge', int, doc="'int' between 0 and 125", minval=0, maxval=125,
...     predicate=classmethod(lambda cls, n: cls.minval <= n < cls.maxval))
...

isinstance() overload

>>> isinstance(1, PositiveInt)
True
>>> isinstance(-1, PositiveInt)
False
>>> isinstance('a', PositiveInt)
False

validate()

>>> PositiveInt.validate(1)  # No output => the value is a valid one
>>> PositiveInt.validate(-1)
Traceback (most recent call last):
 ...
ValueError: expected 'PositiveInt' ('int' > 0) but got -1
>>> PositiveInt.validate('a')
Traceback (most recent call last):
 ...
TypeError: expected 'PositiveInt' ('int' > 0) but got 'str'

register()

>>> isinstance(0, PositiveInt)
False
>>> PositiveInt.validate(0)
Traceback (most recent call last):
 ...
ValueError: expected 'PositiveInt' ('int' > 0) but got 0
>>> PositiveInt.register(0)  # Now let pass 0
>>> isinstance(0, PositiveInt)
True
>>> PositiveInt.validate(0)

As Descriptor

>>> class Circle:
...     radius = PositiveInt()
...
>>> c = Circle()
>>> c.radius = 1
>>> c.radius = -1
Traceback (most recent call last):
 ...
ValueError: expected 'PositiveInt' ('int' > 0) but got -1 for 'radius' attribute of 'Circle' object
>>> c.radius = 'a'
Traceback (most recent call last):
 ...
TypeError: expected 'PositiveInt' ('int' > 0) but got 'str' for 'radius' attribute of 'Circle' object

From existing annotations

>>> from checktypes import checktyped
>>> @checktyped
... class Point2D:
...     x: float
...     y: float
...
>>> p = Point2D()
>>> p.x = 0.0
>>> p.y = 1.0
>>> p.x = 'a'
Traceback (most recent call last):
 ...
TypeError: expected 'float' but got 'str' for 'x' attribute of 'Point2D' object

For further documentation, see the module docstrings.

For other examples, see Recipes.md

TODO

  • Improve module docstrings.
  • Add recipes.

Project details


Download files

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

Files for checktypes, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size checktypes-0.2.2.tar.gz (7.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page