typical: Python's Typing Toolkit
![Netlify Status](https://api.netlify.com/api/v1/badges/982a0ced-bb7f-4391-87e8-1957071d2f66/deploy-status)
![How Typical](static/typical.png)
Introduction
Typical is a library devoted to runtime analysis, inference,
validation, and enforcement of Python types,
PEP 484 Type Hints, and
custom user-defined data-types.
Typical is fully compliant with the following Python Typing PEPs:
It provides a high-level Protocol API, Functional API, and Object API to suit most any
occasion.
Getting Started
Installation is as simple as pip install -U typical
.
Help
The latest documentation is hosted at
python-typical.org.
Starting with version 2.0, All documentation is hand-crafted
markdown & versioned documentation can be found at typical's
Git Repo.
(Versioned documentation is still in-the-works directly on our
domain.)
A Typical Use-Case
The decorator that started it all:
typic.al(...)
import typic
@typic.al
def hard_math(a: int, b: int, *c: int) -> int:
return a + b + sum(c)
hard_math(1, "3")
@typic.al(strict=True)
def strict_math(a: int, b: int, *c: int) -> int:
return a + b + sum(c)
strict_math(1, 2, 3, "4")
Typical has both a high-level Object API and high-level
Functional API. In general, any method registered to one API is also
available to the other.
The Protocol API
import dataclasses
from typing import Iterable
import typic
@typic.constrained(ge=1)
class ID(int):
...
@typic.constrained(max_length=280)
class Tweet(str):
...
@dataclasses.dataclass
class Tweeter:
id: ID
tweets: Iterable[Tweet]
json = '{"id":1,"tweets":["I don\'t understand Twitter"]}'
protocol = typic.protocol(Tweeter)
t = protocol.transmute(json)
print(t)
print(protocol.tojson(t))
protocol.validate({"id": 0, "tweets": []})
The Functional API
import dataclasses
from typing import Iterable
import typic
@typic.constrained(ge=1)
class ID(int):
...
@typic.constrained(max_length=280)
class Tweet(str):
...
@dataclasses.dataclass
class Tweeter:
id: ID
tweets: Iterable[Tweet]
json = '{"id":1,"tweets":["I don\'t understand Twitter"]}'
t = typic.transmute(Tweeter, json)
print(t)
print(typic.tojson(t))
typic.validate(Tweeter, {"id": 0, "tweets": []})
The Object API
from typing import Iterable
import typic
@typic.constrained(ge=1)
class ID(int):
...
@typic.constrained(max_length=280)
class Tweet(str):
...
@typic.klass
class Tweeter:
id: ID
tweets: Iterable[Tweet]
json = '{"id":1,"tweets":["I don\'t understand Twitter"]}'
t = Tweeter.transmute(json)
print(t)
print(t.tojson())
Tweeter.validate({"id": 0, "tweets": []})
Changelog
See our
Releases.