Typical: Python's Typing Toolkit.
Project description
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.
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
File details
Details for the file typical-2.0.11.tar.gz
.
File metadata
- Download URL: typical-2.0.11.tar.gz
- Upload date:
- Size: 70.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.7.6 Linux/5.0.0-1036-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5b21ab90c71608cd4f5ba1070633e31887034741e1f3fcf39dcc38f6bab6a10 |
|
MD5 | 29e08c534f4aff394e26d23336db1116 |
|
BLAKE2b-256 | 51a3c0a76b0da0d51baf985b72dd0449ac2238a29bbfc94937d71752ab7c7f68 |
File details
Details for the file typical-2.0.11-py3-none-any.whl
.
File metadata
- Download URL: typical-2.0.11-py3-none-any.whl
- Upload date:
- Size: 87.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.5 CPython/3.7.6 Linux/5.0.0-1036-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a7810d993f71707a311ef3c52069373b9c0d83d508554784b791114cc140acc |
|
MD5 | da5ed88c30501e3799a03e36f8a5730e |
|
BLAKE2b-256 | b3b9acbbfbaca4ba6a95dd7497a0bfb0f499e6c43e14d91a785726078626fb61 |