Skip to content

perf: avoid importing Pydantic v1 for v2 handlers - #604

Merged
yeongseon merged 1 commit into
mainfrom
perf/lazy-v1-model-rejection
Oct 26, 2026
Merged

yeongseon merged 1 commit into
mainfrom
perf/lazy-v1-model-rejection

Conversation

@yeongseon

Copy link
Copy Markdown
Owner

Summary

Avoid importing the legacy Pydantic namespace solely to reject legacy models. A legacy class cannot exist without its module already being loaded, so consult sys.modules and retain real subclass recognition rather than module-name guessing.

Changes

  • Preserve informative v1 rejection for all six model options and subclasses.
  • Add a fresh-process regression proving normal v2 decoration leaves pydantic.v1 unloaded; it failed before this fix.

Validation

  • Python 3.11 make check-all build: 764 passed, 27 deselected, 4 warnings in 19.51s; 97.16% coverage.
  • Existing v1 rejection, custom adapter and v2 behavior tests pass unchanged.
  • Repeatable import-cost probe: hatch run python -X importtime -c "import pydantic.v1". On Python 3.11.15/Pydantic 2.14.1 this measured 58.676 ms cumulative including 35.388 ms for pydantic itself (about 23.3 ms incremental legacy import). This is one local observation, not a cross-version performance guarantee; GitHub matrix verifies behavior on supported versions.
  • No new dependencies or public options. GitHub CI pending.

Related Issues

Closes #594

Merge authorized by the maintainer’s explicit instruction.

@yeongseon

Copy link
Copy Markdown
Owner Author

Merge authorized by the maintainer’s explicit instruction. Reviewed sys.modules-based identity against existing v1 subclasses and v2 cold-import regression. Exact head b1204db: all five required contexts completed=success, no unresolved threads. Python 3.11 local make check-all build: 764 passed, 27 deselected, 4 warnings in 19.51s; 97.16% coverage.

@yeongseon
yeongseon merged commit e0ec300 into main Oct 26, 2026
25 checks passed
@yeongseon
yeongseon deleted the perf/lazy-v1-model-rejection branch October 26, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(import): avoid eager pydantic.v1 import solely for rejecting v1 models

1 participant