A library of types that simplify working with typed Python.
Project description
Types that make validating types in Python easy.
typet works best with Python 3.6 or later. Prior to 3.6, validation types are supported, but the object types cannot be supported until typingplus supports class type comments.
Installation
Install it using pip:
pip install typet
Features
Validation types that can be used while casting to validate an object’s content.
Basic Usage
One of the cooler features of typet is the ability to create complex objects with very little code. The following code creates an object that generates properties from the annotated class attributes that will ensure that only values of int or that can coerced into int can be set. It also generates a full suite of common comparison methods.
from typet import Object
class Point(Object):
x: int
y: int
p1 = Point(0, 0) # Point(x=0, y=0)
p2 = Point('2', 2.5) # Point(x=2, y=2)
assert p1 < p2 # True
Additionally, typet contains a suite of sliceable classes that will create bounded, or validated, versions of those types that always assert their values are within bounds; however, when an instance of a bounded type is instantiated, the instance will be of the original type.
from typet import Bounded
BoundedInt = Bounded[int, 10:20]
x = BoundedInt(15) # Okay
type(x) # <class 'int'>
BoundedInt(5) # Raises ValueError
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for typet-0.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dca54ec96b18aed08bfa5dcef7defed126d4ac58b52aa0d9d517f629ee85c9b |
|
MD5 | 0798fafc10f6efab49b5fb4902622757 |
|
BLAKE2b-256 | 3540e019d339b510c9276b11fd4ea5426b0bc66490899348734b95aa2a6f5f0c |