|
| 1 | +about_resource: pydantic_core-2.41.5.tar.gz |
| 2 | +name: pydantic-core |
| 3 | +version: 2.41.5 |
| 4 | +download_url: https://files.pythonhosted.org/packages/71/70/23b021c950c2addd24ec408e9ab05d59b035b39d97cdc1130e1bce647bb6/pydantic_core-2.41.5.tar.gz |
| 5 | +description: | |
| 6 | + Core functionality for Pydantic validation and serialization |
| 7 | + # pydantic-core |
| 8 | + |
| 9 | + [](https://github.com/pydantic/pydantic-core/actions?query=event%3Apush+branch%3Amain+workflow%3Aci) |
| 10 | + [](https://codecov.io/gh/pydantic/pydantic-core) |
| 11 | + [](https://pypi.python.org/pypi/pydantic-core) |
| 12 | + [](https://github.com/pydantic/pydantic-core) |
| 13 | + [](https://github.com/pydantic/pydantic-core/blob/main/LICENSE) |
| 14 | + |
| 15 | + This package provides the core functionality for [pydantic](https://docs.pydantic.dev) validation and serialization. |
| 16 | + |
| 17 | + Pydantic-core is currently around 17x faster than pydantic V1. |
| 18 | + See [`tests/benchmarks/`](./tests/benchmarks/) for details. |
| 19 | + |
| 20 | + ## Example of direct usage |
| 21 | + |
| 22 | + _NOTE: You should not need to use pydantic-core directly; instead, use pydantic, which in turn uses pydantic-core._ |
| 23 | + |
| 24 | + ```py |
| 25 | + from pydantic_core import SchemaValidator, ValidationError |
| 26 | + |
| 27 | + |
| 28 | + v = SchemaValidator( |
| 29 | + { |
| 30 | + 'type': 'typed-dict', |
| 31 | + 'fields': { |
| 32 | + 'name': { |
| 33 | + 'type': 'typed-dict-field', |
| 34 | + 'schema': { |
| 35 | + 'type': 'str', |
| 36 | + }, |
| 37 | + }, |
| 38 | + 'age': { |
| 39 | + 'type': 'typed-dict-field', |
| 40 | + 'schema': { |
| 41 | + 'type': 'int', |
| 42 | + 'ge': 18, |
| 43 | + }, |
| 44 | + }, |
| 45 | + 'is_developer': { |
| 46 | + 'type': 'typed-dict-field', |
| 47 | + 'schema': { |
| 48 | + 'type': 'default', |
| 49 | + 'schema': {'type': 'bool'}, |
| 50 | + 'default': True, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + ) |
| 56 | + |
| 57 | + r1 = v.validate_python({'name': 'Samuel', 'age': 35}) |
| 58 | + assert r1 == {'name': 'Samuel', 'age': 35, 'is_developer': True} |
| 59 | + |
| 60 | + # pydantic-core can also validate JSON directly |
| 61 | + r2 = v.validate_json('{"name": "Samuel", "age": 35}') |
| 62 | + assert r1 == r2 |
| 63 | + |
| 64 | + try: |
| 65 | + v.validate_python({'name': 'Samuel', 'age': 11}) |
| 66 | + except ValidationError as e: |
| 67 | + print(e) |
| 68 | + """ |
| 69 | + 1 validation error for model |
| 70 | + age |
| 71 | + Input should be greater than or equal to 18 |
| 72 | + [type=greater_than_equal, context={ge: 18}, input_value=11, input_type=int] |
| 73 | + """ |
| 74 | + ``` |
| 75 | + |
| 76 | + ## Getting Started |
| 77 | + |
| 78 | + ### Prerequisites |
| 79 | + |
| 80 | + You'll need: |
| 81 | + 1. **[Rust](https://rustup.rs/)** - Rust stable (or nightly for coverage) |
| 82 | + 2. **[uv](https://docs.astral.sh/uv/getting-started/installation/)** - Fast Python package manager (will install Python 3.9+ automatically) |
| 83 | + 3. **[git](https://git-scm.com/)** - For version control |
| 84 | + 4. **[make](https://www.gnu.org/software/make/)** - For running development commands (or use `nmake` on Windows) |
| 85 | + |
| 86 | + ### Quick Start |
| 87 | + |
| 88 | + ```bash |
| 89 | + # Clone the repository (or from your fork) |
| 90 | + git clone git@github.com:pydantic/pydantic-core.git |
| 91 | + cd pydantic-core |
| 92 | + |
| 93 | + # Install all dependencies using uv, setup pre-commit hooks, and build the development version |
| 94 | + make install |
| 95 | + ``` |
| 96 | + |
| 97 | + Verify your installation by running: |
| 98 | + |
| 99 | + ```bash |
| 100 | + make |
| 101 | + ``` |
| 102 | + |
| 103 | + This runs a full development cycle: formatting, building, linting, and testing |
| 104 | + |
| 105 | + ### Development Commands |
| 106 | + |
| 107 | + Run `make help` to see all available commands, or use these common ones: |
| 108 | + |
| 109 | + ```bash |
| 110 | + make build-dev # to build the package during development |
| 111 | + make build-prod # to perform an optimised build for benchmarking |
| 112 | + make test # to run the tests |
| 113 | + make testcov # to run the tests and generate a coverage report |
| 114 | + make lint # to run the linter |
| 115 | + make format # to format python and rust code |
| 116 | + make all # to run to run build-dev + format + lint + test |
| 117 | + ``` |
| 118 | + |
| 119 | + ### Useful Resources |
| 120 | + |
| 121 | + * [`python/pydantic_core/_pydantic_core.pyi`](./python/pydantic_core/_pydantic_core.pyi) - Python API types |
| 122 | + * [`python/pydantic_core/core_schema.py`](./python/pydantic_core/core_schema.py) - Core schema definitions |
| 123 | + * [`tests/`](./tests) - Comprehensive usage examples |
| 124 | + |
| 125 | + ## Profiling |
| 126 | + |
| 127 | + It's possible to profile the code using the [`flamegraph` utility from `flamegraph-rs`](https://github.com/flamegraph-rs/flamegraph). (Tested on Linux.) You can install this with `cargo install flamegraph`. |
| 128 | + |
| 129 | + Run `make build-profiling` to install a release build with debugging symbols included (needed for profiling). |
| 130 | + |
| 131 | + Once that is built, you can profile pytest benchmarks with (e.g.): |
| 132 | + |
| 133 | + ```bash |
| 134 | + flamegraph -- pytest tests/benchmarks/test_micro_benchmarks.py -k test_list_of_ints_core_py --benchmark-enable |
| 135 | + ``` |
| 136 | + The `flamegraph` command will produce an interactive SVG at `flamegraph.svg`. |
| 137 | + |
| 138 | + ## Releasing |
| 139 | + |
| 140 | + 1. Bump package version locally. Do not just edit `Cargo.toml` on Github, you need both `Cargo.toml` and `Cargo.lock` to be updated. |
| 141 | + 2. Make a PR for the version bump and merge it. |
| 142 | + 3. Go to https://github.com/pydantic/pydantic-core/releases and click "Draft a new release" |
| 143 | + 4. In the "Choose a tag" dropdown enter the new tag `v<the.new.version>` and select "Create new tag on publish" when the option appears. |
| 144 | + 5. Enter the release title in the form "v<the.new.version> <YYYY-MM-DD>" |
| 145 | + 6. Click Generate release notes button |
| 146 | + 7. Click Publish release |
| 147 | + 8. Go to https://github.com/pydantic/pydantic-core/actions and ensure that all build for release are done successfully. |
| 148 | + 9. Go to https://pypi.org/project/pydantic-core/ and ensure that the latest release is published. |
| 149 | + 10. Done 🎉 |
| 150 | +homepage_url: https://github.com/pydantic/pydantic-core |
| 151 | +package_url: pkg:pypi/pydantic-core@2.41.5 |
| 152 | +license_expression: mit |
| 153 | +copyright: Copyright Samuel Colvin |
| 154 | +attribute: yes |
| 155 | +checksum_md5: 54a367c4549ec48a8b3a63d38e821506 |
| 156 | +licenses: |
| 157 | + - key: mit |
| 158 | + name: MIT License |
| 159 | + file: mit.LICENSE |
0 commit comments