Package: typical
typical: Python's Typing ToolkitIntroductionTypical is a library devoted to runtime analysis, inference,
validation, and enforcement of Python types,PEP 484Type Hints, and
custom user-defined data-types.Typical is fully compliant with the following Python Typing PEPs:PEP 484 -- Type HintsPEP 563 -- Postponed Evaluation of AnnotationsPEP 585 -- Type Hinting Generics In Standard CollectionsPEP 586 -- Literal TypesPEP 589 -- TypedDict: Type Hints for Dictionaries with a Fixed Set of KeysPEP 604 -- Allow writing union types as X | YIt provides a high-level Protocol API, Functional API, and Object API to suit most any
occasion.Getting StartedInstallation is as simple aspip install -U typical.HelpThe latest documentation is hosted atpython-typical.org.Starting with version 2.0, All documentation is hand-crafted
markdown & versioned documentation can be found at typical'sGit Repo.
(Versioned documentation is still in-the-works directly on our
domain.)A Typical Use-CaseThe decorator that started it all:typic.al(...)importtypic@typic.aldefhard_math(a:int,b:int,*c:int)->int:returna+b+sum(c)hard_math(1,"3")#> 4@typic.al(strict=True)defstrict_math(a:int,b:int,*c:int)->int:returna+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-levelObject APIand high-levelFunctional API. In general, any method registered to one API is also
available to the other.The Protocol APIimportdataclassesfromtypingimportIterableimporttypic@typic.constrained(ge=1)classID(int):...@typic.constrained(max_length=280)classTweet(str):...@dataclasses.dataclass# or typing.TypedDict or typing.NamedTuple or annotated class...classTweeter:id:IDtweets: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)The Functional APIimportdataclassesfromtypingimportIterableimporttypic@typic.constrained(ge=1)classID(int):...@typic.constrained(max_length=280)classTweet(str):...@dataclasses.dataclass# or typing.TypedDict or typing.NamedTuple or annotated class...classTweeter:id:IDtweets: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)The Object APIfromtypingimportIterableimporttypic@typic.constrained(ge=1)classID(int):...@typic.constrained(max_length=280)classTweet(str):...@typic.klassclassTweeter:id:IDtweets: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)ChangelogSee ourReleases.