Skip to main content

A library of types that simplify working with typed Python.

Project description

PyPI Python Versions Build Status Coverage Status Code Quality

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


Download files

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

Source Distribution

typet-0.0.1.tar.gz (12.3 kB view hashes)

Uploaded Source

Built Distribution

typet-0.0.1-py2.py3-none-any.whl (11.8 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page