Fix nested type imports in x-python-type override#2842
Conversation
📝 WalkthroughWalkthroughAdds regex-based extraction of type names from x-python-type strings and extends the JSON Schema parser to collect/import nested types referenced inside x-python-type annotations; includes tests and expected outputs for nested built-in and unknown custom nested types. Changes
Sequence Diagram(s)(Skipped — changes are localized to parser import-collection logic and tests; no new multi-component sequential flow requiring visualization.) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🧬 Code graph analysis (1)tests/data/expected/main/jsonschema/x_python_type_nested_unknown_type.py (1)
🪛 Ruff (0.14.10)tests/data/expected/main/jsonschema/x_python_type_nested_unknown_type.py14-14: Undefined name (F821) ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
🔇 Additional comments (3)
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. Comment |
|
📚 Docs Preview: https://pr-2842.datamodel-code-generator.pages.dev |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/jsonschema.py (1)
1338-1343: Remove unusednoqadirective.The
# noqa: PLR6301comment is unnecessary since thePLR6301rule is not enabled, as indicated by static analysis.🔎 Suggested fix
- def _extract_all_type_names(self, type_str: str) -> list[str]: # noqa: PLR6301 + def _extract_all_type_names(self, type_str: str) -> list[str]:
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
tests/data/jsonschema/x_python_type_nested_imports.jsonis excluded by!tests/data/**/*.jsonand included by none
📒 Files selected for processing (3)
src/datamodel_code_generator/parser/jsonschema.pytests/data/expected/main/jsonschema/x_python_type_nested_imports.pytests/main/jsonschema/test_main_jsonschema.py
🧰 Additional context used
🧬 Code graph analysis (3)
tests/main/jsonschema/test_main_jsonschema.py (2)
tests/test_main_kr.py (1)
output_file(44-46)tests/main/conftest.py (2)
output_file(98-100)run_main_and_assert(244-408)
tests/data/expected/main/jsonschema/x_python_type_nested_imports.py (1)
src/datamodel_code_generator/model/typed_dict.py (1)
TypedDict(49-114)
src/datamodel_code_generator/parser/jsonschema.py (3)
src/datamodel_code_generator/types.py (1)
DataType(296-805)src/datamodel_code_generator/imports.py (1)
append(74-89)src/datamodel_code_generator/parser/base.py (1)
data_type(1094-1096)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/parser/jsonschema.py
1338-1338: Unused noqa directive (non-enabled: PLR6301)
Remove unused noqa directive
(RUF100)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
- GitHub Check: 3.13 on Ubuntu
- GitHub Check: 3.13 on Windows
- GitHub Check: py312-isort7 on Ubuntu
- GitHub Check: 3.12 on Windows
- GitHub Check: py312-pydantic1 on Ubuntu
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.11 on Windows
- GitHub Check: benchmarks
- GitHub Check: Analyze (python)
🔇 Additional comments (4)
src/datamodel_code_generator/parser/jsonschema.py (2)
11-11: LGTM!The
reimport is correctly added to support the new_extract_all_type_namesmethod.
1368-1379: LGTM! The nested import collection logic correctly addresses the issue.The implementation properly extracts nested type names and adds corresponding imports from
PYTHON_TYPE_IMPORTS. The approach of extendingresult.data_typeswith import-carryingDataTypeinstances ensures the generated code will include the necessary imports.Note: If the same type appears multiple times in an annotation (e.g.,
Callable[[Iterable[str], Iterable[int]], str]), duplicateDataTypeentries will be created. This is harmless since the import system deduplicates via sets inImports.append().tests/main/jsonschema/test_main_jsonschema.py (1)
7036-7044: New x-python-type nested imports test is consistent and targetedThe test wiring (input path, fixtures,
extra_args) matches the existingx_python_type_*tests and cleanly exercises the new nested-import behavior forCallable[[Iterable[str]], str]. No issues from a test-structure perspective.tests/data/expected/main/jsonschema/x_python_type_nested_imports.py (1)
1-14: Expected TypedDict output correctly models nested Callable importsThe generated stub cleanly includes all required imports (
Callable,Iterable,TypedDict,NotRequired) and theModeldefinition matches the intendedcallback: NotRequired[Callable[[Iterable[str]], str]]shape. This aligns with the new nested x-python-type handling.
CodSpeed Performance ReportMerging #2842 will not alter performanceComparing
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2842 +/- ##
=======================================
Coverage 99.49% 99.49%
=======================================
Files 90 90
Lines 14336 14353 +17
Branches 1713 1717 +4
=======================================
+ Hits 14264 14281 +17
Misses 37 37
Partials 35 35
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR is a bug fix that adds missing import statements for nested types in x-python-type annotations. Previously, using x-python-type with nested types like Callable[[Iterable[str]], str] would generate code missing the Iterable import, causing the generated code to fail. The changes are: (1) a new private method _extract_all_type_names() to parse type strings, and (2) enhanced _get_python_type_override() to collect and add imports for nested types. Both are internal implementation changes. The output change is additive (adds missing required imports) which fixes invalid generated code - this is a bug fix, not a breaking change. No CLI/API changes, no template changes required, no default behavior changes for correctly working code. This analysis was performed by Claude Code Action |
|
🎉 Released in 0.51.0 This PR is now available in the latest release. See the release notes for details. |
Fixes: #2841
When using
x-python-typewith nested types likeCallable[[Iterable[str]], str], theIterablewas not being imported. This PR adds logic to extract all type names from the type annotation string and add imports for any that are inPYTHON_TYPE_IMPORTS.Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.