Skip to content

Import the type expression reader of the model renames where they run, so a run without models does not load it - #6043

Merged
Koudai Aono (koxudaxi) merged 3 commits into
fix1-32from
fix1-33
Nov 3, 2026
Merged

Koudai Aono (koxudaxi) merged 3 commits into
fix1-32from
fix1-33

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor
    • Made a small internal adjustment to how type names are collected. The collection behavior remains unchanged, and there are no user-facing changes.

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The import of iter_python_type_expr_names moves from module scope into _names_written_as_text. The helper’s name collection logic is unchanged.

Changes: Import placement

Layer / File(s) Summary
Local import in helper
src/datamodel_code_generator/parser/_passes/imports.py
The module-level import is removed. _names_written_as_text imports iter_python_type_expr_names locally.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Change: Bug fix

Merge Risk: Minimal · previous review 24192

The localized import change presents no identified merge-blocking issue and is mergeable subject to normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and accurately describes the main change: it moves the type expression reader import to the code path where model renames run, preventing unnecessary loading when no models exist.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
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

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.
Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between d057e152f6e0ff10d1348d62ccb2f5a5252a7411 and 24192e40345fd9b65e9eed9a6de9ea33ae4da371.

Files selected for processing: 1
  • src/datamodel_code_generator/parser/_passes/imports.py

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

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 3, 2026 03:16
@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T03:17:05.413782Z 24192e4 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

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 50.31%

2 warnings

⚠️ 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

⚠️ 52 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 52 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_client_calls[sync] 44.3 ms 24.8 ms +78.7%
⚡ test_perf_simple_msgspec_unset_field_rendering 568.9 ms 322.9 ms +76.19%
⚡ test_perf_client_calls[async] 47.8 ms 28 ms +70.42%
⚡ test_perf_large_models_single_module_stdout 1.7 s 1.1 s +64.42%
⚡ test_perf_scalar_root_collapsed_builtin 220.8 ms 134.3 ms +64.34%
⚡ test_perf_load_pyproject_config[mapping-None] 372.6 µs 227 µs +64.13%
⚡ test_perf_load_pyproject_config[mapping-multiple] 384.6 µs 235.4 µs +63.37%
⚡ test_perf_load_pyproject_config[config-None] 367.5 µs 225 µs +63.36%
⚡ test_perf_ordinary_array_constraint_generation 2.3 s 1.4 s +62.66%
⚡ test_perf_load_pyproject_config[config-multiple] 383.6 µs 237.1 µs +61.81%
⚡ test_perf_large_models_pydantic_v2_builtin_double_quotes 1,559.3 ms 964.2 ms +61.71%
⚡ test_perf_local_reference_file_resolution 148.5 ms 92.1 ms +61.24%
⚡ test_perf_inherited_required_fields[partial-derived-first] 1,257 ms 781.6 ms +60.83%
⚡ test_perf_large_models_msgspec 2.2 s 1.4 s +59.47%
⚡ test_perf_large_models_typed_dict[total-false-requiredness] 1,309.2 ms 833.1 ms +57.15%
⚡ test_perf_large_models_pydantic_v2_noformat 1,419.2 ms 905.3 ms +56.76%
⚡ test_perf_large_models_pydantic_v2_builtin 1,429.1 ms 913.1 ms +56.5%
⚡ test_perf_inherited_required_fields[required-derived-first] 790.1 ms 504.9 ms +56.47%
⚡ test_perf_inherited_required_fields[partial-base-first] 1,125.5 ms 719.6 ms +56.4%
⚡ test_perf_false_reference_validation 296 ms 189.8 ms +55.93%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix1-33 (94e8257) with fix1-32 (1040f53)

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

Hyphenated directory (#6039). A directory is read as importable by its name once its `-` is `_`, as the output writes it
(`written_name`): `café-models` is written to `café_models/` and imported by that name, as the files of 0.88.0 were
written, where the check came before the `-` was replaced and the directory was sanitized as a file's name is
(`caf__models`), so it moved and two directories of the same length (`café-models`, `cafè-models`) shared one package. The
import of such a directory was a syntax error up to 0.88.0 (`from .café-models import a`), so only output that failed to
import changes.

Names an additional import binds (#6041). A type of a module that writes a dotted name writes the first name of it where
the module's names are read (`ipaddress.IPv4Address` binds `ipaddress`), not the last one. The check of the imports every
module writes that a model of the module shadows reads that first name (`iter_python_type_expr_head_names`), so a model
named like the last name of a qualified runtime symbol (`x-python-import` `symbols`) keeps its name, as in 0.88.0. The
name a model or an import takes anew where a shared import binds the name it had skips the names the shared imports bind
(`shared_import_bindings`), so `IPv4Address1` is not given to a model where `mymod.IPv4Address1` is imported by every
module, nor `Status_1` or `list_1` to the import a package names anew.

Enum member defaults (#6040). `set_default_enum_member` writes a member by the expression its type names the enum by when
it runs, so a default kept naming the import that the passes of the imports named anew afterwards (`Status` for
`Status as Status_1`, `list.Pop` for `list_1.Pop`) and the generated module failed to import. The two passes that name an
import anew write the new expression into the members of the defaults that use it (`name_enum_member`, a store mutation
with its row in the fact table).

Item counts (#6037). The item counts that `contains` and boolean `items` imply belong to a field whose schema admits an
array (it declares no type, or an array is one of them) as well as whose type can hold one: a string typed
`datetime.date` and an object with array values (`{"type": "object", "additionalProperties": {"type": "array"}}`) took
`min_length` from them, which raised `Unable to apply constraint` for a date and rejected the maps with fewer keys.
@koxudaxi
Koudai Aono (koxudaxi) merged commit 8a1ecd7 into main Nov 3, 2026
309 of 311 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix1-33 branch November 3, 2026 07:53
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: I read the whole diff. The main behaviour change is in jsonschema.py: a new admits_array check in _assemble_field_constraints stops item counts implied by contains or minContains from becoming length constraints on properties whose schema declares no array type, such as an object map with array values or a string retyped via x-python-type. The behaviour diff gate marks the msgspec cell for the new array_counts_beside_retyped input as accepted because Meta min_length was dropped from the map field. The allowlist update and the new expected output show the same change for Pydantic, so default generated code changes. Separately, _names_written_as_text now checks the head name of a dotted type instead of its last name. Under --additional-imports, a model named like an imported name is therefore no longer renamed when the module only writes that name qualified, which changes the class name for those users. I treated the other changes as fixes to broken output and not as breaking changes. These are: enum member defaults now follow renamed import aliases; import and model renames now skip names already bound by additional imports; hyphenated non-ASCII directory names are now sanitized the same way their files are written. The behaviour gate marks the related cells as crash-fix or both-fail, or they fix outputs that were broken before. The remaining changes are internal helper additions, docs, the fact table and tests. No CLI options, public API, templates or Python version support were removed or changed.

Content for Release Notes

Code Generation Changes

  • Item counts no longer applied to properties whose schema declares no array type - A property whose schema declares a type without array (for example type: object with array-valued additionalProperties, or type: string retyped with x-python-type: datetime.date) no longer gets the minItems/maxItems that contains, minContains or items: false imply as a length constraint. A map field that used to be generated as Annotated[dict[str, list[str]], Meta(min_length=2)] (msgspec) or with Field(None, min_length=2) (Pydantic) is now a plain dict[str, list[str]], so generated models now accept maps with fewer entries, which the schema also accepts. A string retyped as a date no longer gets a length constraint that failed when the model was imported (Import the type expression reader of the model renames where they run, so a run without models does not load it #6043)
  • Models named like an additional import keep their name when types only write the import qualified - With --additional-imports, a model whose name matches an imported name is no longer renamed when the module's types write that name only through its module path (for example a type written as ipaddress.IPv4Address alongside --additional-imports ipaddress.IPv4Address and a model titled IPv4Address). The model cannot shadow the import in that case, so it now keeps its schema name instead of the suffixed name it got before. Code that referenced the previously renamed class must switch to the original name (Import the type expression reader of the model renames where they run, so a run without models does not load it #6043)

This analysis was produced by the release draft workflow

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