Fix exact imports for reused tree-scope models#3042
Conversation
📝 WalkthroughWalkthroughA fix to the reuse-model generation flow that immediately updates child references in models after creating an inherited model, ensuring proper reference migration in the parser. Includes corresponding test data validating the combination of --reuse-scope=tree with --use-exact-imports for Pydantic v2. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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 |
Merging this PR will not alter performance
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/main/jsonschema/test_main_jsonschema.py (1)
5716-5730: Execute the generated package in this exact-imports regression.Line 5716 only snapshots the generated files. Since
#3037is an import-time failure, I’d make this test assert runtime importability too withforce_exec_validation=True(or an explicit import step).Suggested change
run_main_and_assert( input_path=JSON_SCHEMA_DATA_PATH / "reuse_scope_tree", output_path=output_dir, expected_directory=EXPECTED_JSON_SCHEMA_PATH / "reuse_scope_tree_exact_imports", input_file_type="jsonschema", extra_args=[ "--output-model-type", "pydantic_v2.BaseModel", "--reuse-model", "--reuse-scope", "tree", "--use-exact-imports", "--disable-timestamp", ], + force_exec_validation=True, )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/main/jsonschema/test_main_jsonschema.py` around lines 5716 - 5730, The test currently only snapshots generated files via run_main_and_assert for the "reuse_scope_tree" case but doesn't validate runtime importability; adjust the call to run_main_and_assert (the helper used in this test) to enable execution validation by adding force_exec_validation=True so the generated package is actually imported/executed (or alternatively add an explicit import step after generation) to catch the import-time failure seen in issue `#3037`.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/main/jsonschema/test_main_jsonschema.py`:
- Around line 5716-5730: The test currently only snapshots generated files via
run_main_and_assert for the "reuse_scope_tree" case but doesn't validate runtime
importability; adjust the call to run_main_and_assert (the helper used in this
test) to enable execution validation by adding force_exec_validation=True so the
generated package is actually imported/executed (or alternatively add an
explicit import step after generation) to catch the import-time failure seen in
issue `#3037`.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d5aa141e-a93f-419a-b044-abd03aea15bd
📒 Files selected for processing (6)
src/datamodel_code_generator/parser/base.pytests/data/expected/main/jsonschema/reuse_scope_tree_exact_imports/__init__.pytests/data/expected/main/jsonschema/reuse_scope_tree_exact_imports/schema_a.pytests/data/expected/main/jsonschema/reuse_scope_tree_exact_imports/schema_b.pytests/data/expected/main/jsonschema/reuse_scope_tree_exact_imports/shared.pytests/main/jsonschema/test_main_jsonschema.py
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3042 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 85 85
Lines 17688 17692 +4
Branches 2068 2068
=========================================
+ Hits 17688 17692 +4
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 corrects incorrect code generation when using --use-exact-imports with --reuse-scope=tree. The change adds calls to replace_children_in_models() to ensure that when models are reused (creating inherited aliases), other models that reference the original model get their references properly updated to point to the new inherited model. Bug fixes that correct broken behavior are not breaking changes - they are corrections that make the tool produce valid output. No API, CLI, template, default behavior, or Python version changes were made. This analysis was performed by Claude Code Action |
|
🎉 Released in 0.55.0 This PR is now available in the latest release. See the release notes for details. |
Fixes: #3037
Summary by CodeRabbit
Bug Fixes
Tests