Skip to content

Commit f43fab3

Browse files
authored
move pydantic_core -> python/pydantic_core (#705)
1 parent 424a2f8 commit f43fab3

File tree

16 files changed

+37
-55
lines changed

16 files changed

+37
-55
lines changed

.cargo/config.toml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
[build]
2-
rustflags = [
3-
"-A", "incomplete_features",
4-
]
2+
rustflags = []
53

64
# see https://pyo3.rs/main/building_and_distribution.html#macos
75
[target.x86_64-apple-darwin]
86
rustflags = [
97
"-C", "link-arg=-undefined",
108
"-C", "link-arg=dynamic_lookup",
11-
"-A", "incomplete_features",
129
]
1310

1411
[target.aarch64-apple-darwin]
1512
rustflags = [
1613
"-C", "link-arg=-undefined",
1714
"-C", "link-arg=dynamic_lookup",
18-
"-A", "incomplete_features",
1915
]

.github/workflows/ci.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
- run: ls -lha
5454
- run: coverage xml
5555

56-
- run: coverage-prepare lcov pydantic_core/*.so
56+
- run: coverage-prepare lcov python/pydantic_core/*.so
5757

5858
- uses: codecov/codecov-action@v3
5959

@@ -151,8 +151,7 @@ jobs:
151151
python-version: '3.11'
152152

153153
- run: pip install -r tests/requirements.txt
154-
- run: 'pip install "maturin>=1,<2" typing_extensions'
155-
- run: make build-dev
154+
- run: pip install -e . --config-settings=build-args='--profile dev'
156155

157156
- run: pip freeze
158157
- run: pytest
@@ -464,8 +463,6 @@ jobs:
464463
fi
465464
run: |
466465
set -x
467-
# this is required so that pytest uses the installed pydantic-core package
468-
rm -r pydantic_core
469466
# typing-extensions isn't automatically installed because of `--no-index --no-deps`
470467
python3 -m venv venv
471468
source venv/bin/activate
@@ -499,7 +496,6 @@ jobs:
499496
name: pypi_files
500497
path: dist
501498

502-
- run: rm -r pydantic_core
503499
- run: pip install typing-extensions
504500
- run: pip install -r tests/requirements.txt
505501
- run: pip install pydantic-core --no-index --no-deps --find-links dist --force-reinstall

.github/workflows/codspeed.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ jobs:
4747
uses: Swatinem/rust-cache@v2
4848

4949
- name: Install pydantic-core
50-
# build-prod does not include MiMalloc because it bypasses Maturin
50+
# --no-default-features to avoid using mimalloc
5151
run: |
52-
pip install -e .
53-
make build-prod
54-
python -c 'import pydantic_core'
52+
pip install -e . --config-settings=build-args='--no-default-features --verbose' -v
53+
python -c 'import pydantic_core; assert pydantic_core._pydantic_core.__pydantic_core_default_allocator__'
5554
env:
5655
CONST_RANDOM_SEED: 0 # Fix the compile time RNG seed
5756

58-
# this is required so that pytest uses the installed package
59-
- run: rm tests/__init__.py
60-
6157
- name: Run CodSpeed benchmarks
6258
uses: CodSpeedHQ/action@v1
6359
with:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ node_modules/
3434
/*.profdata
3535
/*.profraw
3636
/foobar.py
37-
/pydantic_core/*.so
37+
/python/pydantic_core/*.so
3838
/src/self_schema.py

Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ include = [
2626
]
2727

2828
[dependencies]
29-
pyo3 = "0.19.0"
29+
pyo3 = { version = "0.19.0", features = ["generate-import-lib", "num-bigint"] }
3030
regex = "1.6.0"
3131
strum = { version = "0.25.0", features = ["derive"] }
3232
strum_macros = "0.24.3"
3333
serde_json = {version = "1.0.87", features = ["preserve_order"]}
3434
enum_dispatch = "0.3.8"
3535
serde = "1.0.147"
36-
mimalloc = { version = "0.1.30", default-features = false, optional = true }
36+
# disabled for benchmarks since it makes microbenchmark performance more flakey
37+
mimalloc = { version = "0.1.30", optional = true, default-features = false, features = ["local_dynamic_tls"] }
3738
speedate = "0.9.0"
3839
ahash = "0.8.0"
3940
url = "2.3.1"
@@ -49,11 +50,7 @@ crate-type = ["cdylib", "rlib"]
4950
[features]
5051
# must be enabled when building with `cargo build`, maturin enables this automatically
5152
extension-module = ["pyo3/extension-module"]
52-
# required for cargo bench
53-
auto-initialize = ["pyo3/auto-initialize"]
54-
# disabled for benchmarks since it makes microbenchmark performance more flakey
55-
mimalloc-allocator = ["mimalloc", "mimalloc/local_dynamic_tls"]
56-
default = ["pyo3/generate-import-lib", "pyo3/num-bigint"]
53+
default = ["mimalloc"]
5754

5855
[profile.release]
5956
lto = "fat"

Makefile

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.DEFAULT_GOAL := all
2-
black = black pydantic_core tests generate_self_schema.py wasm-preview/run_tests.py
3-
ruff = ruff pydantic_core tests generate_self_schema.py wasm-preview/run_tests.py
2+
black = black python/pydantic_core tests generate_self_schema.py wasm-preview/run_tests.py
3+
ruff = ruff python/pydantic_core tests generate_self_schema.py wasm-preview/run_tests.py
44

55
.PHONY: install
66
install:
@@ -17,28 +17,18 @@ install-rust-coverage:
1717

1818
.PHONY: build-dev
1919
build-dev:
20-
@rm -f pydantic_core/*.so
21-
cargo build --features extension-module
22-
@rm -f target/debug/lib_pydantic_core.d
23-
@rm -f target/debug/lib_pydantic_core.rlib
24-
@mv target/debug/lib_pydantic_core.* pydantic_core/_pydantic_core.so
20+
@rm -f python/pydantic_core/*.so
21+
maturin develop
2522

2623
.PHONY: build-prod
2724
build-prod:
28-
@rm -f pydantic_core/*.so
29-
cargo build --release --features extension-module
30-
@rm -f target/release/lib_pydantic_core.d
31-
@rm -f target/release/lib_pydantic_core.rlib
32-
@mv target/release/lib_pydantic_core.* pydantic_core/_pydantic_core.so
25+
@rm -f python/pydantic_core/*.so
26+
maturin develop --release
3327

3428
.PHONY: build-coverage
3529
build-coverage:
36-
pip uninstall -y pydantic_core
37-
rm -f pydantic_core/*.so
38-
RUSTFLAGS='-C instrument-coverage -A incomplete_features' cargo build --features extension-module
39-
@rm -f target/debug/lib_pydantic_core.d
40-
@rm -f target/debug/lib_pydantic_core.rlib
41-
mv target/debug/lib_pydantic_core.* pydantic_core/_pydantic_core.so
30+
rm -f python/pydantic_core/*.so
31+
maturin develop -- -C instrument-coverage
4232

4333
.PHONY: build-wasm
4434
build-wasm:
@@ -56,7 +46,7 @@ format:
5646
lint-python:
5747
$(ruff)
5848
$(black) --check --diff
59-
griffe dump -f -d google -LWARNING -o/dev/null pydantic_core
49+
griffe dump -f -d google -LWARNING -o/dev/null python/pydantic_core
6050

6151
.PHONY: lint-rust
6252
lint-rust:
@@ -110,7 +100,7 @@ testcov: build-coverage
110100
coverage run -m pytest
111101
coverage report
112102
coverage html -d htmlcov/python
113-
coverage-prepare html pydantic_core/*.so
103+
coverage-prepare html python/pydantic_core/*.so
114104

115105
.PHONY: all
116106
all: format build-dev lint test
@@ -145,4 +135,4 @@ clean:
145135
rm -f .coverage.*
146136
rm -rf build
147137
rm -rf perf.data*
148-
rm -rf pydantic_core/*.so
138+
rm -rf python/pydantic_core/*.so

generate_self_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This script generates the schema for the schema - e.g.
33
a definition of what inputs can be provided to `SchemaValidator()`.
44
5-
The schema is generated from `pydantic_core/core_schema.py`.
5+
The schema is generated from `python/pydantic_core/core_schema.py`.
66
"""
77
from __future__ import annotations as _annotations
88

@@ -34,7 +34,7 @@
3434
else:
3535
# can't import core_schema.py directly as pydantic-core might not be installed
3636
core_schema_spec = importlib.util.spec_from_file_location(
37-
'_typing', str(THIS_DIR / 'pydantic_core' / 'core_schema.py')
37+
'_typing', str(THIS_DIR / 'python' / 'pydantic_core' / 'core_schema.py')
3838
)
3939
core_schema = importlib.util.module_from_spec(core_schema_spec)
4040
core_schema_spec.loader.exec_module(core_schema)

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,17 @@ Funding = 'https://github.com/sponsors/samuelcolvin'
4444
Source = 'https://github.com/pydantic/pydantic-core'
4545

4646
[tool.maturin]
47+
python-source = "python"
4748
module-name = "pydantic_core._pydantic_core"
4849
bindings = 'pyo3'
49-
features = ["pyo3/extension-module", "mimalloc-allocator"]
50+
features = ["pyo3/extension-module"]
5051

5152
[tool.ruff]
5253
line-length = 120
5354
extend-select = ['Q', 'RUF100', 'C90', 'I']
5455
flake8-quotes = {inline-quotes = 'single', multiline-quotes = 'double'}
5556
mccabe = { max-complexity = 13 }
56-
isort = { known-first-party = ['pydantic-core', 'tests'] }
57+
isort = { known-first-party = ['pydantic_core', 'tests'] }
5758

5859
[tool.pytest.ini_options]
5960
testpaths = 'tests'
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate core;
44

55
use pyo3::prelude::*;
66

7-
#[cfg(feature = "mimalloc-allocator")]
7+
#[cfg(feature = "mimalloc")]
88
#[global_allocator]
99
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
1010

@@ -63,5 +63,9 @@ fn _pydantic_core(py: Python, m: &PyModule) -> PyResult<()> {
6363
m.add_function(wrap_pyfunction!(to_json, m)?)?;
6464
m.add_function(wrap_pyfunction!(to_jsonable_python, m)?)?;
6565
m.add_function(wrap_pyfunction!(list_all_errors, m)?)?;
66+
67+
#[cfg(not(feature = "mimalloc"))]
68+
m.setattr("__pydantic_core_default_allocator__", true)?; // uses setattr so this is not in __all__
69+
6670
Ok(())
6771
}

tests/test_docstrings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def find_examples(*_directories):
1313

1414

1515
@pytest.mark.skipif(sys.platform not in {'linux', 'darwin'}, reason='Only on linux and macos')
16-
@pytest.mark.parametrize('example', find_examples('pydantic_core/core_schema.py'), ids=str)
16+
@pytest.mark.parametrize('example', find_examples('python/pydantic_core/core_schema.py'), ids=str)
1717
def test_docstrings(example: CodeExample, eval_example: EvalExample):
1818
eval_example.set_config(quotes='single')
1919

tests/test_errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def test_all_errors():
402402
error_types = [e['type'] for e in errors]
403403
if error_types != list(core_schema.ErrorType.__args__):
404404
literal = ''.join(f'\n {e!r},' for e in error_types)
405-
print(f'python code (end of pydantic_core/core_schema.py):\n\nErrorType = Literal[{literal}\n]')
405+
print(f'python code (end of python/pydantic_core/core_schema.py):\n\nErrorType = Literal[{literal}\n]')
406406
pytest.fail('core_schema.ErrorType needs to be updated')
407407

408408

tests/test_misc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ def get_type_value(schema):
160160
schema_types = tuple(dict.fromkeys(schema_types)) # remove duplicates while preserving order
161161
if get_args(CoreSchemaType) != schema_types:
162162
literal = ''.join(f'\n {e!r},' for e in schema_types)
163-
print(f'python code (near end of pydantic_core/core_schema.py):\n\nCoreSchemaType = Literal[{literal}\n]')
163+
print(
164+
f'python code (near end of python/pydantic_core/core_schema.py):\n\nCoreSchemaType = Literal[{literal}\n]'
165+
)
164166
pytest.fail('core_schema.CoreSchemaType needs to be updated')
165167

166168

0 commit comments

Comments
 (0)