Skip to content

Import the class of another module that TypedDict extra_items names - #5219

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-4822-any-importfrom
fix-5031-extra-items
Oct 23, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-4822-any-importfrom
fix-5031-extra-items

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Fixes: #5031

Summary by CodeRabbit

  • Bug Fixes
    • Corrected generated TypedDict definitions so extra_items references resolve correctly when their types are defined in other modules or use finalized class names.

@coderabbitai

coderabbitai Bot commented Oct 23, 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. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 557a2969e2fd6f91740f362afa350aec98b9062d and 57e70d8c4e8c9d2e44deac44356f6d7bbba06298.

⛔ Files ignored due to path filters: 4
  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/expected/main/openapi/main_openapi/typed_dict_extra_items_modules.payloads.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/openapi/typed_dict_extra_items_final_name/modules.yaml is excluded by !tests/data/**/*.yaml and included by none
  • tests/data/payloads/typed_dict_extra_items_modules.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing: 13
  • docs/architecture.md
  • src/datamodel_code_generator/model/base.py
  • src/datamodel_code_generator/model/typed_dict.py
  • src/datamodel_code_generator/parser/_pass_decls.py
  • src/datamodel_code_generator/parser/_passes/_shared.py
  • src/datamodel_code_generator/parser/base.py
  • tests/data/expected/main/openapi/main_openapi/typed_dict_extra_items_modules/__init__.py
  • tests/data/expected/main/openapi/main_openapi/typed_dict_extra_items_modules/a.py
  • tests/data/expected/main/openapi/main_openapi/typed_dict_extra_items_modules/b.py
  • tests/data/expected/main/openapi/main_openapi/typed_dict_extra_items_modules/c.py
  • tests/main/openapi/test_main_openapi.py
  • tests/main/openapi/test_main_openapi.refs
  • tests/test_yaml_fast_constructor.refs

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

📝 Walkthrough

TypedDict extra_items data types now participate in parser name collection and final-class resolution. The additional-properties naming pass reads import aliases. New modular OpenAPI fixtures and tests cover references to classes in other modules.

Changes: TypedDict extra_items cross-module references

Layer / File(s) Summary
Collect extra_items data types
src/datamodel_code_generator/model/base.py, src/datamodel_code_generator/model/typed_dict.py, src/datamodel_code_generator/parser/_passes/_shared.py, src/datamodel_code_generator/parser/base.py
DataModel provides a default hook for setting data types. TypedDict supplies the source types for extra_items, and parser name collection includes backend-setting types.
Resolve final names through imports
src/datamodel_code_generator/parser/_pass_decls.py, docs/architecture.md
The additional-properties naming pass reads import aliases as well as final class names. The architecture documentation describes the related import and name-resolution behavior.
Validate modular output
tests/data/expected/main/openapi/main_openapi/typed_dict_extra_items_modules/*, tests/main/openapi/test_main_openapi.py, tests/main/openapi/test_main_openapi.refs, tests/test_yaml_fast_constructor.refs
Generated fixtures cover extra_items references to classes in other modules. The OpenAPI test checks schema and Pydantic validation, and the fixture references include the new cases.

Priority: ➖ Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

Merge Risk: ⚪ Minimal · up to 57e70

No merge-blocking issue is established; the generated modules can proceed through 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 describes the main change: importing a class from another module when a TypedDict extra_items reference names it.
Linked Issues check Passed Issue #5031 requires TypedDict.extra_items references to use the referenced module and to participate in import processing. The changes add TypedDict setting data types to name collection and make…
Out of Scope Changes check Passed The changes stay within issue #5031. Source changes process extra_items through existing type and import passes. Documentation explains that behavior. Tests and expected outputs verify modular `Type…
Docstring Coverage Passed Docstring coverage is 92.31% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 9 files. (4 skipped: 3 …
Full details: Linked Issues check

Explanation

Issue #5031 requires TypedDict.extra_items references to use the referenced module and to participate in import processing. The changes add TypedDict setting data types to name collection and make final-class rendering read import aliases. The expected modular output uses b.Bar and imports b. The new OpenAPI test covers modular TypedDict output and schema validation. The excluded fixture files limit direct inspection of test inputs, but the reviewed test and expected-output changes establish the required case.

Full details: Out of Scope Changes check

Explanation

The changes stay within issue #5031. Source changes process extra_items through existing type and import passes. Documentation explains that behavior. Tests and expected outputs verify modular TypedDict generation. No unrelated functional change is identified.

Full details: Docstring Coverage

Explanation

Docstring coverage is 92.31% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 9 files. (4 skipped: 3 unsupported, 1 too large.)

✨ 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

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-10-23T14:10:44.832776Z d50be4f PR opened
ℹ️ 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

@codspeed

codspeed Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 36.04%

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

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

⚡ 50 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_unique_items_nested_model_validation 2.5 ms 1.7 ms +53.27%
⚡ test_perf_pydantic_field_name_resolution 1,455.3 µs 969.6 µs +50.1%
⚡ test_perf_unique_items_runtime_scalar_validation 2.3 ms 1.6 ms +48.57%
⚡ test_perf_inherited_required_fields[required-base-first] 625.3 ms 422.6 ms +47.98%
⚡ test_perf_false_reference_validation 261 ms 177.5 ms +47.09%
⚡ test_perf_empty_pydantic_v2_field_render_plans 32.6 ms 22.4 ms +45.52%
⚡ test_perf_inherited_required_fields[required-derived-first] 555.4 ms 382.7 ms +45.14%
⚡ test_perf_ordinary_array_constraint_generation 1.8 s 1.3 s +43.86%
⚡ test_perf_inherited_required_fields[partial-base-first] 922.6 ms 641.8 ms +43.76%
⚡ test_perf_aws_style_openapi_pydantic_v2 1.7 s 1.2 s +43.12%
⚡ test_perf_duplicate_names 814.6 ms 571 ms +42.67%
⚡ test_perf_graphql_style_pydantic_v2 661.3 ms 465.1 ms +42.2%
⚡ test_perf_extreme_large_schema 12.3 s 8.7 s +41.65%
⚡ test_perf_simple_pydantic_v2_field_construction 8.5 ms 6 ms +41.37%
⚡ test_perf_simple_msgspec_unset_field_rendering 110.6 ms 78.6 ms +40.75%
⚡ test_perf_local_reference_file_resolution 121.3 ms 86.3 ms +40.6%
⚡ test_perf_complex_refs 1.7 s 1.2 s +40.26%
⚡ test_perf_multiple_files_input 2.9 s 2.1 s +39.89%
⚡ test_perf_deep_nested 4.4 s 3.2 s +38.17%
⚡ test_perf_inherited_required_fields[partial-derived-first] 862.2 ms 626.4 ms +37.63%
... ... ... ... ...

ℹ️ 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 fix-5031-extra-items (22fa43a) with fix-4822-any-import (557a296)2

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

  2. No successful run was found on fix-4822-any-import (68a22fd) during the generation of this report, so d5334e2 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

extra_items wrote the bare class name of its data type, which the module import pass never visited. In modular
output a class of another module was then undefined, or the class of the same name in the module was named instead
(a.Bar for b.Bar). The import pass now visits the data types of backend settings beside those of fields and
validator rules, so extra_items names the class through its import (b.Bar with from . import b), as field types do.

Fixes: #5031
@koxudaxi
Koudai Aono (koxudaxi) merged commit 4c3105a into main Oct 23, 2026
26 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the fix-5031-extra-items branch October 23, 2026 16:07
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR fixes a bug in modular output that only shows up when TypedDict extra_items names a class in another module. Before the fix, extra_items used the bare class name. That name was undefined in modules that did not import the class, and in one case it pointed to a different class with the same name in the current module instead of the class the schema references. Now the referenced module is imported and extra_items uses the qualified name, such as b.Bar, the same way field types are imported. The code changes add an internal hook, _setting_data_types, on the base and TypedDict models, and a private helper, _data_types_beside_fields, which the import and name-collection passes use. The pass declaration now also reads IMPORT_ALIASES. The rest of the PR is documentation and new tests. No CLI options, public Python API, default values, templates, supported Python versions or error handling change. The generated output changes only where the old output was invalid or did not match the schema, so this is a bug fix and not a breaking change.


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.

TypedDict extra_items in modular output refers to the wrong class and has no import

1 participant