typical: Python's Typing Toolkit
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.
It provides a high-level Functional API and Object API to suite most any occasion.
Getting Started
Installation is as simple as pip install -U typical. For more
installation options to make typical even faster, see the
Install section in the
documentation.
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")
#> 4
@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")
#> Traceback (most recent call last):
#> ...
#> typic.constraints.error.ConstraintValueError: Given value <'4'> fails constraints: (type=int, nullable=False, coerce=False)
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 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)
#> Tweeter(id=1, tweets=["I don't understand Twitter"])
print(t.tojson())
#> '{"id":1,"tweets":["I don\'t understand Twitter"]}'
Tweeter.validate({"id": 0, "tweets": []})
#> Traceback (most recent call last):
#> ...
#> typic.constraints.error.ConstraintValueError: Given value <0> fails constraints: (type=int, nullable=False, coerce=False, ge=1)
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 # or typing.TypedDict or typing.NamedTuple or annotated class...
class Tweeter:
id: ID
tweets: Iterable[Tweet]
json = '{"id":1,"tweets":["I don\'t understand Twitter"]}'
protocol = typic.protocol(Tweeter)
t = protocol.transmute(json) # or typic.transmute()
print(t)
#> Tweeter(id=1, tweets=["I don't understand Twitter"])
print(protocol.tojson(t))
#> '{"id":1,"tweets":["I don\'t understand Twitter"]}'
protocol.validate({"id": 0, "tweets": []}) # or typic.validate()
#> Traceback (most recent call last):
#> ...
#> typic.constraints.error.ConstraintValueError: Tweeter.id: value <0> fails constraints: (type=int, nullable=False, coerce=False, ge=1)
Changelog
See our Change Log and our Releases.
Release files for typical 2.0.14
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| typical-2.0.14.tar.gz | 70.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| typical-2.0.14-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 158.7 kB
Release files / typical-2.0.14.tar.gz
| Download URL | typical-2.0.14.tar.gz |
|---|---|
| Size | 70.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
28263ea5d0a6a307901d6367b16680ee178afa830ef5f356a8fa6c68c956ab36
|
|
BLAKE2b-256 checksum How to use checksums |
ee056deaffee76289fb3bc18c9702f7b411c9936bbb30bb428a10d8e0ca5ff96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.5 CPython/3.7.7 Linux/5.3.0-1020-azure
|
Release files / typical-2.0.14-py3-none-any.whl
| Download URL | typical-2.0.14-py3-none-any.whl |
|---|---|
| Size | 88.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2764e9b1e5a3889067f2a0fb70cff33bc4a792021bb8f30e193951ab0608c0c0
|
|
BLAKE2b-256 checksum How to use checksums |
797317a91ef2467ee8d6d07d403a8bfb2076733f6de7bf0b221714a1ce03a820
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.0.5 CPython/3.7.7 Linux/5.3.0-1020-azure
|