Skip to content

Fix missing Field import with multiple aliases on required fields#2992

Merged
koxudaxi merged 1 commit intomainfrom
fix/multiple-aliases-required-field-import
Feb 14, 2026
Merged

Fix missing Field import with multiple aliases on required fields#2992
koxudaxi merged 1 commit intomainfrom
fix/multiple-aliases-required-field-import

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Feb 14, 2026

Fixes: #2989

Summary by CodeRabbit

Release Notes

  • New Features
    • Added support for multiple validation aliases on required fields in Pydantic v2 models. Fields can now accept data using any of several alternative names through the AliasChoices mechanism.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Feb 14, 2026

📝 Walkthrough

Walkthrough

Removed a fast-path optimization in BaseModel.imports that skipped adding the Field import for simple, non-aliased, non-nullable, non-constrained fields. Field import is now always included when field representation is present, ensuring it's available when aliases create AliasChoices requirements.

Changes

Cohort / File(s) Summary
Base Model Import Logic
src/datamodel_code_generator/model/pydantic/base_model.py
Removed conditional fast-path that bypassed Field import for simple fields; now deterministically adds Field import whenever self.field is present.
Test Data — Dynamic Models
tests/data/expected/dynamic_models/multiple_aliases_required_code.py
New expected output file showing Customer class with full_name field using AliasChoices for multiple validation aliases on a required field.
Test Data — JSON Schema
tests/data/expected/main/jsonschema/jsonschema_multiple_aliases_required_pydantic_v2.py
New expected output file showing Root model with two fields using AliasChoices for multiple validation aliases in Pydantic v2.
Test Cases
tests/main/jsonschema/test_main_jsonschema.py, tests/main/test_dynamic_models.py
Added three new test cases validating multiple aliases on required fields: one for JSON schema main generation and two for dynamic model generation (snapshot and code output variants).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

breaking-change-analyzed

Poem

🐰 A field was missing, lost without a trace,
When aliases made AliasChoices take their place,
No more the shortcut steals the import's light—
Now Field appears when needed, always right! ✨

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: addressing a missing Field import when multiple aliases are used with required fields, which directly aligns with the changeset.
Linked Issues check ✅ Passed The PR successfully fixes issue #2989 by removing the early fast-path in BaseModel.imports that was skipping Field import for certain field types, ensuring Field is always included when needed for AliasChoices.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the missing Field import issue: core logic fix, expected test output files, and comprehensive test coverage with no unrelated modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/multiple-aliases-required-field-import

No actionable comments were generated in the recent review. 🎉


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 and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

📚 Docs Preview: https://pr-2992.datamodel-code-generator.pages.dev

@koxudaxi koxudaxi merged commit 24b576e into main Feb 14, 2026
34 of 35 checks passed
@koxudaxi koxudaxi deleted the fix/multiple-aliases-required-field-import branch February 14, 2026 04:08
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b58970a) to head (e863084).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #2992   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           94        94           
  Lines        18051     18061   +10     
  Branches      2091      2090    -1     
=========================================
+ Hits         18051     18061   +10     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix that adds the missing Field import when validation_aliases (AliasChoices) is used on required fields in Pydantic v2. Previously, the code would generate Field(..., validation_alias=AliasChoices(...)) but fail to include Field in the imports, causing runtime NameError. The generated code structure and behavior remain unchanged - this simply ensures the import statement matches what the code generator produces. No API, CLI, templates, or default behaviors are modified.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

🎉 Released in 0.54.0

This PR is now available in the latest release. See the release notes for details.

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.

Dynamic generation NameError with AliasChoices + Fields

1 participant