Fix use_union_operator with Python builtin type field names#2968
Fix use_union_operator with Python builtin type field names#2968
Conversation
📝 WalkthroughWalkthroughAdds Python-builtin-aware name collision detection to the parser and renames conflicting model fields to "_" while preserving original names via Pydantic Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested labels
Suggested reviewers
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)
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-2968.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging this PR will not alter performanceComparing
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@tests/main/openapi/test_main_openapi.py`:
- Around line 4715-4727: The test test_main_builtin_type_field_names is
asserting code that uses PEP 604 union operator but doesn't pass the flag to
enable it; update the extra_args array in that test (the extra_args passed into
run_main_and_assert) to include "--use-union-operator" so output uses the `|`
operator (matching bool | None, dict[str, Any] | None, etc.) when run with
"--output-model-type pydantic_v2.BaseModel".
🧹 Nitpick comments (1)
src/datamodel_code_generator/parser/base.py (1)
2164-2164: Remove unusednoqadirective.The static analysis tool indicates that
PLR0912is not enabled in the project's linter configuration, making this directive unnecessary.Suggested fix
- def __change_field_name( # noqa: PLR0912 + def __change_field_name(
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2968 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 94 94
Lines 17813 17859 +46
Branches 2055 2061 +6
=========================================
+ Hits 17813 17859 +46
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:
|
3279508 to
a3988fc
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@src/datamodel_code_generator/parser/base.py`:
- Line 2164: Remove the unused noqa PLR0912 suppression on the private function
__change_field_name: edit the function definition for __change_field_name to
delete the trailing "# noqa: PLR0912" comment so the linter warning is not
masked and the code uses normal linting rules for that function.
- Around line 2212-2235: The builtin-shadowing check should operate on the
current field.name (which may have been normalized by ModelResolver) rather than
the original filed_name, and only assign field.alias when no alias already
exists; update the condition from checking filed_name to checking field.name
against _BUILTIN_NAMES, use field.name in the per-type checks (dt.type,
dt.is_list, etc.) to determine should_rename, and if should_rename set
field.name = f"{field.name}_" and set field.alias = filed_name only if
field.alias is None so existing aliases (e.g., from snake_case_field
normalization) are preserved; add a regression test for a case like original
"Float" with snake_case_field=True to assert alias remains "Float" and name
becomes "float_".
Breaking Change AnalysisResult: Breaking changes detected Reasoning: This PR changes generated code output in a way that affects existing users. Before the PR, fields like Content for Release NotesCode Generation Changes
This analysis was performed by Claude Code Action |
|
🎉 Released in 0.54.0 This PR is now available in the latest release. See the release notes for details. |
Fixes: #2964
Summary by CodeRabbit
Bug Fixes
Tests