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
A close equivalent traditional class would be much larger, would have to be updated for any new attributes, and wouldn’t support more advanced casting, such as to types annotated using the typing module:
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Point(x={x}, y={y})'.format(x=self.x, y=self.y)
def __setattr__(self, name, value):
if name in ('x', 'y'):
value = int(value)
super(Point, self).__setattr__(name, value)
def __eq__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return (self.x, self.y) == (other.x, other.y)
def __ne__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return not self == other
def __lt__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return (self.x, self.y) < (other.x, other.y)
def __le__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return self == other or self < other
def __gt__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return not self <= other
def __ge__(self, other):
if other.__class__ is not self.__class__:
return NotImplemented
return not self < other
def __hash__(self):
return hash((self.x, self.y))
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
For more advanced example let’s combine these to create a class that checks bounds and contains an optional attribute.
from typet import Object, Bounded
class Person(Object):
name: str
age: Bounded[int, 0:150]
hobby: str = None
Person('Jimothy', 23) # Okay, hobby will be None
Person('Jimothy', 230) # Raises ValueError
Person('Jimothy', 23, 'Figure Skating') # Okay, and sets hobby
Future Usage for Python 2.7 to 3.5
In the future, typet will support class type comments for annotations.
from typet import Object
class Point(Object):
x = None # type: int
y = None # type: int
p1 = Point(0, 0) # Point(x=0, y=0)
p2 = Point('2', 2.5) # Point(x=2, y=2)
assert p1 < p2 # True
Note that, because Python prior to 3.6 cannot annotate an attribute without defining it, by convention, this will not imply a type of Optional[int]. If the type is Optional[int], it must be specified explicitly in the type comment.
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.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffa56a3cb682b0cf475950de781f5fdf9ad9fe4d9be018335ca3e0ebba7aeb6b |
|
MD5 | 0cd7d0dce24a29ab73e84a36dcf9047d |
|
BLAKE2b-256 | 7280ac46a448d6fa8999076001c411c3eb089ab40aa34dc36ed57988cb2d7cd5 |