Typical:Python的Typing工具包。
项目描述
典型:Python的Typing工具包
简介
Typical是一个致力于运行时分析、推断、验证和强制执行Python类型、PEP 484类型提示和自定义用户定义数据类型的库,PEP 484类型提示。
Typical完全符合以下Python类型PEP
- PEP 484 -- 类型提示
- PEP 563 -- 延迟评估注解
- PEP 585 -- 标准集合中的类型提示泛型
- PEP 586 -- 文本类型
- PEP 589 -- TypedDict:具有固定键集的字典的类型提示
- PEP 604 -- 允许将联合类型写为X | Y
它提供了一套高级协议API、函数API和对象API,以适应各种场合。
入门
安装很简单,只需pip install -U typical
。
帮助
最新文档托管在python-typical.org。
从版本2.0开始,所有文档均为手工编写的Markdown格式,版本化文档可以在typical的Git仓库找到。(版本化文档还在我们域名上直接制作中。)
典型用例
启动一切的装饰器
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具有高级的对象API和高级的功能API。通常,注册到一个API的任何方法都可以在另一个API中使用。
协议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)
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": []})
#> Traceback (most recent call last):
#> ...
#> typic.constraints.error.ConstraintValueError: Tweeter.id: value <0> fails constraints: (type=int, nullable=False, coerce=False, ge=1)
功能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"]}'
t = typic.transmute(Tweeter, json)
print(t)
#> Tweeter(id=1, tweets=["I don't understand Twitter"])
print(typic.tojson(t))
#> '{"id":1,"tweets":["I don\'t understand Twitter"]}'
typic.validate(Tweeter, {"id": 0, "tweets": []})
#> Traceback (most recent call last):
#> ...
#> typic.constraints.error.ConstraintValueError: Tweeter.id: value <0> fails constraints: (type=int, nullable=False, coerce=False, ge=1)
对象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)
变更日志
查看我们的发布。
项目详情
下载文件
下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。
源分发
typical-2.9.0.tar.gz (90.0 kB 查看哈希值)
构建分发
typical-2.9.0-py3-none-any.whl (107.9 kB 查看哈希值)
关闭
typical-2.9.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b8fcf86dce410c59cedd0c4a2a80d1b70e11bbe6fe343b81bfa5b303eedc5343 |
|
MD5 | b9662e5a51fb412c15e89cb950a5eecf |
|
BLAKE2b-256 | f303f9460181600e15b303920ba5fe02ab6b55048214416f8812457073b61944 |
关闭
typical-2.9.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3cd23f6dc8b28f3ffaafeed1aa159e36fd64a999907dec492a359734524ae498 |
|
MD5 | 38949dfd05210df7a953ec50a3ad2754 |
|
BLAKE2b-256 | 45afbc9dbafd2bb7bf03449aed06208e0fb2ffc98abadff14d9f4f5df69ecfcc |