Package: pydantic-core
pydantic-coreThis package provides the core functionality forpydanticvalidation and serialization.Pydantic-core is currently around 17x faster than pydantic V1.
Seetests/benchmarks/for details.Example of direct usageNOTE: You should not need to use pydantic-core directly; instead, use pydantic, which in turn uses pydantic-core.frompydantic_coreimportSchemaValidator,ValidationErrorv=SchemaValidator({'type':'typed-dict','fields':{'name':{'type':'typed-dict-field','schema':{'type':'str',},},'age':{'type':'typed-dict-field','schema':{'type':'int','ge':18,},},'is_developer':{'type':'typed-dict-field','schema':{'type':'default','schema':{'type':'bool'},'default':True,},},},})r1=v.validate_python({'name':'Samuel','age':35})assertr1=={'name':'Samuel','age':35,'is_developer':True}# pydantic-core can also validate JSON directlyr2=v.validate_json('{"name": "Samuel", "age": 35}')assertr1==r2try:v.validate_python({'name':'Samuel','age':11})exceptValidationErrorase:print(e)"""1 validation error for modelageInput should be greater than or equal to 18[type=greater_than_equal, context={ge: 18}, input_value=11, input_type=int]"""Getting StartedYou'll need rust stableinstalled, or rust nightly if you want to generate accurate coverage.With rust and python 3.8+ installed, compiling pydantic-core should be possible with roughly the following:# clone this repo or your forkgitclonegit@github.com:pydantic/pydantic-core.gitcdpydantic-core# create a new virtual envpython3-mvenvenvsourceenv/bin/activate# install dependencies and install pydantic-coremakeinstallThat should be it, the example shown above should now run.You might find it useful to look atpython/pydantic_core/_pydantic_core.pyiandpython/pydantic_core/core_schema.pyfor more information on the python API,
beyond that,tests/provide a large number of examples of usage.If you want to contribute to pydantic-core, you'll want to use some other make commands:make build-devto build the package during developmentmake build-prodto perform an optimised build for benchmarkingmake testto run the testsmake testcovto run the tests and generate a coverage reportmake lintto run the lintermake formatto format python and rust codemaketo runformat build-dev lint testProfilingIt's possible to profile the code using theflamegraphutility fromflamegraph-rs. (Tested on Linux.) You can install this withcargo install flamegraph.Runmake build-profilingto install a release build with debugging symbols included (needed for profiling).Once that is built, you can profile pytest benchmarks with (e.g.):flamegraph--pytesttests/benchmarks/test_micro_benchmarks.py-ktest_list_of_ints_core_py--benchmark-enableTheflamegraphcommand will produce an interactive SVG atflamegraph.svg.ReleasingBump package version locally. Do not just editCargo.tomlon Github, you need bothCargo.tomlandCargo.lockto be updated.Make a PR for the version bump and merge it.Go tohttps://github.com/pydantic/pydantic-core/releasesand click "Draft a new release"In the "Choose a tag" dropdown enter the new tagv<the.new.version>and select "Create new tag on publish" when the option appears.Enter the release title in the form "v<the.new.version> "Click Generate release notes buttonClick Publish releaseGo tohttps://github.com/pydantic/pydantic-core/actionsand ensure that all build for release are done successfully.Go tohttps://pypi.org/project/pydantic-core/and ensure that the latest release is published.Done 🎉