Rename a msgspec field named "field" to avoid shadowing the field import#3615
Rename a msgspec field named "field" to avoid shadowing the field import#3615chuenchen309 wants to merge 1 commit into
Conversation
msgspec models emit `from msgspec import ... field` and call `field(...)` for aliases, defaults, etc. A schema property named "field" was rendered as a class attribute `field`, shadowing that import, so any later `field(...)` call raised `TypeError: 'msgspec._core.Field' object is not callable` and the generated module could not be imported. Add a msgspec-specific field-name resolver (mirroring the Pydantic one) that renames a "field" property to `field_` with a `name="field"` alias, and route msgspec Structs to it via field_name_model_type. Adds a regression test that generates and imports the model. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (4)
📝 WalkthroughWalkthroughMsgspec Struct models now use a dedicated field-name strategy that prevents generated identifiers from shadowing ChangesMsgspec field resolution
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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 |
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3615 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 165 165
Lines 36372 36380 +8
Branches 4199 4200 +1
=========================================
+ Hits 36372 36380 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A schema property named
fieldgenerates a msgspecStructwhose class body shadows thefrom msgspec import fieldused for other fields'field(...)calls, so the generated module can't even be imported:A property named
fieldis common (form/validation payloads,{field, message}error objects), so this is easy to hit via the public API and the CLI.msgspec routed field names through the base resolver, which never renames anything; only pydantic had a reserved-name-aware resolver. This adds a
MsgspecFieldNameResolver(mirroringPydanticFieldNameResolver) that renames afieldproperty tofield_with aname="field"alias — the wire name is preserved and the generated module imports and round-trips. Added a regression test that generates and imports the model.Scope: I kept this to the msgspec
fieldcase that crashes. The pydantic-v2Fieldimport is the same class of issue but a wider change (Fieldis used pervasively) — happy to handle it in a follow-up, or here if you'd prefer.Disclosure: this PR was authored by an AI coding agent (Claude Code) running on this account — it found the shadowing, reproduced the failed import, wrote the fix and the test, and wrote this description. The account holder reviews every change and is accountable for it, and the verification above is re-runnable from the diff. Happy to close it if it isn't the kind of contribution you want.
Summary by CodeRabbit
Bug Fixes
field.fieldhelper, while preserving the original schema field name.Tests