Fix generic type import with module path#2858
Conversation
📝 WalkthroughWalkthroughEnhanced the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
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-2858.datamodel-code-generator.pages.dev |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/data/python/input_model/pydantic_models.py (1)
16-19: Optional: Simplify generic inheritance.Inheriting from
UserDict[TK, TV]already makes the class generic, so the explicitGeneric[TK, TV]is redundant. The preferred form would be:class CustomGenericDict(UserDict[TK, TV]): """Custom generic dict for testing generic type import.""" passHowever, if you're intentionally testing edge cases with redundant
Genericinheritance, this is fine to keep.🔎 Proposed simplification
-class CustomGenericDict(UserDict[TK, TV], Generic[TK, TV]): +class CustomGenericDict(UserDict[TK, TV]): """Custom generic dict for testing generic type import.""" pass
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/datamodel_code_generator/__main__.pytests/data/python/input_model/pydantic_models.pytests/test_input_model.py
⏰ 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). (15)
- GitHub Check: Analyze (python)
- GitHub Check: benchmarks
- GitHub Check: py312-black22 on Ubuntu
- GitHub Check: py312-isort6 on Ubuntu
- GitHub Check: py312-isort7 on Ubuntu
- GitHub Check: 3.14 on Ubuntu
- GitHub Check: 3.12 on Windows
- GitHub Check: py312-isort5 on Ubuntu
- GitHub Check: 3.12 on Ubuntu
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.13 on Windows
- GitHub Check: py312-black24 on Ubuntu
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.11 on Windows
- GitHub Check: build-deploy
🔇 Additional comments (2)
src/datamodel_code_generator/__main__.py (1)
691-709: LGTM! Clean implementation for module-qualified origin names.The enhanced logic correctly returns fully qualified names (
module.name) for custom generic origins while preserving simple names for standard library types. The safety checks (if module and module not in {...}andif name:) properly handle edge cases, and the fallback logic remains intact.tests/test_input_model.py (1)
730-742: LGTM! Comprehensive test coverage for custom generic imports.The test properly validates that custom generic types are imported with their full module paths and used correctly in type annotations. The assertions cover both required and optional field scenarios.
CodSpeed Performance ReportMerging #2858 will not alter performanceComparing
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2858 +/- ##
=======================================
Coverage 99.52% 99.52%
=======================================
Files 90 90
Lines 14929 14935 +6
Branches 1787 1788 +1
=======================================
+ Hits 14858 14864 +6
Misses 38 38
Partials 33 33
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 for the --input-model feature. The change modifies _get_origin_name() to return fully qualified names (module.ClassName) for custom generic types instead of just the class name. Previously, using custom generic types (like CustomGenericDict from a user module) with --input-model would generate code that referenced the type without the necessary import, causing broken output. The fix ensures proper imports are generated. Since the previous behavior was broken (generated non-working code), this is not a breaking change - it's a fix that makes the feature work correctly for custom generics. 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. |
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.