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

```python
>>> 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 `lambda`s:

```python
>>> 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

```python
>>> 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


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

```

### `validate()`

```python
>>> 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()`

```python
>>> 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

```python
>>> 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

```python
>>> 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 or the help of CheckType.
##### For other examples, see [Recipes.md](https://gitlab.com/yahya-abou-imran/checktypes/blob/master/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.3
Filename, size File type Python version Upload date Hashes
Filename, size checktypes-0.2.3.tar.gz (5.2 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