Skip to content

Fix pydantic typed extra runtime compatibility#3601

Merged
koxudaxi merged 2 commits into
mainfrom
fix/pydantic-extra-runtime-compatibility
Jul 11, 2026
Merged

Fix pydantic typed extra runtime compatibility#3601
koxudaxi merged 2 commits into
mainfrom
fix/pydantic-extra-runtime-compatibility

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Bug Fixes
    • Improved generated Pydantic models for additionalProperties “typed extras”, including self-references with nullable/optional handling and correct behavior when targeting newer Python versions and disabling future imports.
    • Enhanced forward-reference handling for these typed extras so generated models rebuild cleanly.
  • Tests
    • Added end-to-end JSON Schema regression tests for Python 3.13/3.14 and multiple typed-extra self-reference strategies.
  • Chores
    • Expanded the Pydantic regression test matrix to include pydantic213 and extended shared pytest coverage cases.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 26b30927-4f62-4688-80ec-a7b7c744dc76

📥 Commits

Reviewing files that changed from the base of the PR and between b8b4039 and bc0eac5.

📒 Files selected for processing (1)
  • tests/main/jsonschema/test_main_jsonschema.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/main/jsonschema/test_main_jsonschema.py

📝 Walkthrough

Walkthrough

The parser centralizes deferred-annotation handling for typed extras and tracks aliased forward references more precisely. New JSON Schema fixtures and regression tests cover self-references and union operators, while tox and coverage matrices add Pydantic 2.13.

Changes

Typed extras handling

Layer / File(s) Summary
Centralize typed-extra annotation mode
src/datamodel_code_generator/parser/base.py
Parser helpers determine deferred-annotation usage, configure typed-extra templates, and update forward-reference alias tracking.
Validate generated typed extras
tests/data/expected/main/jsonschema/*, tests/main/jsonschema/test_main_jsonschema.py
Fixtures and regression tests cover self-referential extras, nullable values, union operators, Python 3.13 without future imports, and Python 3.14.
Expand Pydantic regression environments
tox.ini, .github/workflows/test.yaml
Adds the Pydantic 2.13 environment and includes the expanded JSON Schema tests in coverage runs.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested reviewers: gaborbernat

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing Pydantic typed-extra runtime compatibility.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pydantic-extra-runtime-compatibility

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@koxudaxi koxudaxi marked this pull request as ready for review July 11, 2026 05:15
@github-actions

Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-3601.datamodel-code-generator.pages.dev

@codspeed-hq

codspeed-hq Bot commented Jul 11, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 14.11%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 13 regressed benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_large_models_pydantic_v2_noformat 667.3 ms 844.7 ms -21%
WallTime test_perf_large_models_pydantic_v2_builtin 728.9 ms 863.1 ms -15.54%
WallTime test_perf_deep_nested 4.3 s 5 s -14.88%
WallTime test_perf_all_options_enabled 4.4 s 5.2 s -14.3%
WallTime test_perf_duplicate_names 869.7 ms 1,013.6 ms -14.2%
WallTime test_perf_multiple_files_input 2.9 s 3.4 s -14.17%
WallTime test_perf_large_models_pydantic_v2 2.9 s 3.4 s -13.79%
WallTime test_perf_openapi_large 2.4 s 2.8 s -13.76%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.6 s 1.9 s -13.21%
WallTime test_perf_complex_refs 1.7 s 2 s -12.8%
WallTime test_perf_kubernetes_style_pydantic_v2 2.2 s 2.5 s -12.47%
WallTime test_perf_graphql_style_pydantic_v2 663.9 ms 754.3 ms -11.98%
WallTime test_perf_stripe_style_pydantic_v2 1.7 s 1.9 s -10.92%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix/pydantic-extra-runtime-compatibility (bc0eac5) with main (1828eff)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (1828eff) to head (bc0eac5).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3601   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          164       164           
  Lines        35498     35526   +28     
  Branches      4092      4094    +2     
=========================================
+ Hits         35498     35526   +28     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit a425913 into main Jul 11, 2026
63 of 64 checks passed
@koxudaxi koxudaxi deleted the fix/pydantic-extra-runtime-compatibility branch July 11, 2026 10:58
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3601 ("Fix pydantic typed extra runtime compatibility") is a bug fix, not a breaking change. It reworks internal selection of the typed-extra annotation mode so generated code for typed-extra self/forward-referencing Pydantic v2 models is runtime-safe (keeping forward refs quoted, computing deferred-annotation state from with_import/disable_future_imports). No CLI options, public classes, enum values, or documented API functions are removed in the diff — the only rename (use_pydantic_extra_annotations_dict → is_pydantic_extra_field) is an internal getattr-accessed field attribute. Generated output changes only for previously-broken typed-extra cases, turning invalid runtime code into working code, which is a fix rather than a regression. No Python version support is dropped (a pydantic213/Python 3.14 test env is added). Remaining changes are tests, fixtures, and CI/tox config.


This analysis was performed by Claude Code Action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant