Skip to content

Build type-map entries without validating their values again - #6746

Merged
Koudai Aono (koxudaxi) merged 1 commit into
publish-one-approvalfrom
perf-typemap2
Nov 12, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
publish-one-approvalfrom
perf-typemap2

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Improvements
    • Custom type validation is honored when generating types through Pydantic v2 mappings.
    • Standard and Pydantic-specific type mappings continue to select the same types and imports.
  • Refactor
    • Type mapping construction has been streamlined without changing existing mapping behavior.
  • Tests
    • Added coverage for custom type validation in generated mappings.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

The change adds cached construction layouts for context-specific DataType classes and validation helpers for creating data types and import types. Shared and Pydantic v2 type maps now use these helpers. A test checks that an external subclass validator applies to mapped types.

Changes: Type Construction and Mapping

Layer / File(s) Summary
Context-class construction
src/datamodel_code_generator/types.py
DataType delegates union normalization and part linking to _link_parts. Context-specific classes cache validated layouts, which the construction helpers use to create instances directly.
Shared type-map construction
src/datamodel_code_generator/model/types.py
Primitive, standard-library, and optional datetime mappings use _valid_data_type or _valid_import_type. The mapping associations remain unchanged.
Pydantic v2 mappings and validation test
src/datamodel_code_generator/model/pydantic_v2/types.py, tests/test_types.py
Pydantic v2 mappings use _valid_import_type with the existing mapping conditions and imports. The test checks that an external DataType subclass validator applies to integer and password mappings.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

Merge Risk: Minimal · up to a8e1c

This change speeds up type-map construction while keeping the same mapped types and imports. Only a hypothetical undocumented override of from_import could behave differently. No material merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: type-map entries are now built without re-validating their values. The raw summary confirms this. Type-map construction in types.py, pydantic_v2/types.py, and the Dat…
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.
Full details: Title check

Explanation

The title states the main change: type-map entries are now built without re-validating their values. The raw summary confirms this. Type-map construction in types.py, pydantic_v2/types.py, and the DataType layout caching all route through _valid_data_type and _valid_import_type to skip revalidation. The title is specific and matches the commit intent, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T01:29:30.011380Z a8e1c3d Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments: 1

src/datamodel_code_generator/types.py-1794-1794: 1

1794-1794: Minor · Data Integrity & Integration · Quick win

Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.

Preserve omitted arguments when calling an external from_import override.

An unregistered DataType subclass can override from_import with a different default for strict. A type-map call that previously omitted strict now passes strict=False, so the override produces a different mapped type. An override that accepts only the import also now raises TypeError in the shared type-map factory. Forward only arguments that the helper’s caller supplied. (raw.githubusercontent.com)

Review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9fd5ee8 and a8e1c3d.

Files selected for processing: 4

  • src/datamodel_code_generator/model/pydantic_v2/types.py
  • src/datamodel_code_generator/model/types.py
  • src/datamodel_code_generator/types.py
  • tests/test_types.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@codspeed

codspeed Bot commented Nov 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

✅ 53 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing perf-typemap2 (a8e1c3d) with publish-one-approval (9fd5ee8)

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. ↩

@koxudaxi
Koudai Aono (koxudaxi) merged commit 27b6e1c into main Nov 12, 2026
42 of 144 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the perf-typemap2 branch November 12, 2026 04:40
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal speed-up and does not break anything. It adds private helpers (_valid_data_type, _valid_import_type, _build_valid, _link_parts, _valid_layout) that build DataType objects for the internal context classes without running pydantic validation again. The type map factories in model/types.py and model/pydantic_v2/types.py now call these helpers instead of data_type.from_import and data_type(...). The union folding and parent-linking code that ran in DataType.init moved, unchanged, into _link_parts, and init still calls it. The fast path only applies to exact context classes created over DataType. Any other class, including external DataType subclasses, still goes through normal construction and from_import, and a new test checks that their validators still run. No public option, parameter, class, enum value or function was removed or renamed. No CLI options, templates, defaults, Python version support or error handling changed. The behaviour diff for default options lists only both-fail cells, with no accepted or violation cells, so the generated output is the same as in release 0.94.0.


This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.95.0

This PR is now available in the latest release. See the release notes for details.

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