6
6
[ ![ versions] ( https://img.shields.io/pypi/pyversions/pydantic-core.svg )] ( https://github.com/pydantic/pydantic-core )
7
7
[ ![ license] ( https://img.shields.io/github/license/pydantic/pydantic-core.svg )] ( https://github.com/pydantic/pydantic-core/blob/main/LICENSE )
8
8
9
- This package provides the core functionality for pydantic.
9
+ This package provides the core functionality for [ pydantic] ( https://docs.pydantic.dev ) validation and serialization .
10
10
11
- The package is currently a work in progress and subject to significant change.
12
-
13
- There is, as yet, no integration with pydantic, so schemas can only be defined via dictionaries.
14
-
15
- The plan is for pydantic to adopt ` pydantic-core ` in v2 and to generate the schema definition from type hints in
16
- pydantic, then create a ` SchemaValidator ` upon model creation.
17
-
18
- ` pydantic-core ` will be a separate package, required by ` pydantic ` .
11
+ Pydantic-core is currently around 17x faster than pydantic V1.
12
+ See [ ` tests/benchmarks/ ` ] ( ./tests/benchmarks/ ) for details.
19
13
20
- The public interface to pydantic shouldn't change too much as a result of this switch
21
- (though I intend to clean up quite a lot in the public API in v2 as well).
14
+ ## Example of direct usage
22
15
23
- Example of usage:
16
+ _ NOTE: You should not need to use pydantic-core directly, instead use pydantic which which in turn uses pydantic-core. _
24
17
25
18
``` py
26
19
from pydantic_core import SchemaValidator, ValidationError
@@ -74,21 +67,8 @@ except ValidationError as e:
74
67
"""
75
68
```
76
69
77
- Pydantic-core is currently around 17x faster than pydantic standard.
78
- See [ ` tests/benchmarks/ ` ] ( ./tests/benchmarks/ ) for details.
79
-
80
- This relative performance will be less impressive for small models but could be significantly move impressive
81
- for deeply nested models.
82
-
83
- The improvement will decrease slightly when we have to create a class instance after validation,
84
- but shouldn't change more.
85
-
86
- The aim is to remain 10x faster than current pydantic for common use cases.
87
-
88
70
## Getting Started
89
71
90
- While pydantic-core is not yet released and not designed for direct use, you can still try it.
91
-
92
72
You'll need rust stable [ installed] ( https://rustup.rs/ ) , or rust nightly if you want to generate accurate coverage.
93
73
94
74
With rust and python 3.7+ installed, compiling pydantic-core should be possible with roughly the following:
@@ -106,8 +86,8 @@ make install
106
86
107
87
That should be it, the example shown above should now run.
108
88
109
- You might find it useful to look at [ ` pydantic_core/_pydantic_core.pyi ` ] ( ./python/pydantic_core/_pydantic_core.pyi ) and
110
- [ ` pydantic_core/core_schema.py ` ] ( ./python/pydantic_core/core_schema.py ) for more information on the python API,
89
+ You might find it useful to look at [ ` python/ pydantic_core/_pydantic_core.pyi` ] ( ./python/pydantic_core/_pydantic_core.pyi ) and
90
+ [ ` python/ pydantic_core/core_schema.py` ] ( ./python/pydantic_core/core_schema.py ) for more information on the python API,
111
91
beyond that, [ ` tests/ ` ] ( ./tests ) provide a large number of examples of usage.
112
92
113
93
If you want to contribute to pydantic-core, you'll want to use some other make commands:
@@ -118,26 +98,3 @@ If you want to contribute to pydantic-core, you'll want to use some other make c
118
98
* ` make lint ` to run the linter
119
99
* ` make format ` to format python and rust code
120
100
* ` make ` to run ` format build-dev lint test `
121
-
122
- ## Why not JSONSchema?
123
-
124
- Looking at the above schema passed to ` SchemaValidator ` it would seem reasonable to ask "why not use JSONSchema?".
125
-
126
- And if we could use JSONSchema, why not use an existing rust library to do validation?
127
-
128
- In fact, in the very early commits to pydantic-core, I did try to use JSONSchema,
129
- however I quickly realized it wouldn't work.
130
-
131
- JSONSchema does not match the schema for pydantic that closely:
132
- * there are lots of extra checks which pydantic wants to do and aren't covered by JSONSchema
133
- * there are configurations which are possible in JSONSchema but are hard or impossible to imagine in pydantic
134
- * pydantic has the concept of parsing or coercion at it's core, JSONSchema doesn't -
135
- it assumes you either accept or reject the input, never change it
136
- * There are whole classes of problem pydantic has to deal with (like python class instance validation) which JSONSchema
137
- has no idea about since it's dedicated to JSON
138
-
139
- Even if we could use JSONSchema, it wouldn't help much since rust JSONSchema validators expect to know the
140
- schema at compile time, pydantic-core has no knowledge of the schema until ` SchemaValidator ` is initialised.
141
-
142
- Still, it wouldn't be that hard to implement a conversion layer (either in python or rust) to convert JSONSchema
143
- to "pydantic schema" and thereby achieve partial JSONSchema validation.
0 commit comments