Add extends support for profile inheritance#2834
Conversation
📝 WalkthroughWalkthroughAdds profile inheritance resolution for pyproject.toml profiles via a new Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (7)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🧬 Code graph analysis (4)tests/data/expected/main_kr/pyproject_profile/extends_multiple.py (3)
tests/data/expected/main_kr/pyproject_profile/extends_override.py (3)
tests/data/expected/main_kr/pyproject_profile/extends_single.py (3)
tests/test_main_kr.py (4)
⏰ 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)
🔇 Additional comments (10)
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 |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tests/test_main_kr.py (1)
1832-2058: Consider adding a diamond inheritance test.The test suite is comprehensive. One optional addition would be testing diamond inheritance (A extends [B, C] where both B and C extend D) to document the expected behavior when the same ancestor is reachable through multiple paths.
🔎 Example diamond inheritance test
def test_profile_extends_diamond_inheritance(output_file: Path, tmp_path: Path) -> None: """Test diamond inheritance pattern (A extends B and C, both extend D).""" pyproject_toml = """ [tool.datamodel-codegen] target-python-version = "3.10" enable-version-header = false [tool.datamodel-codegen.profiles._base] snake-case-field = true [tool.datamodel-codegen.profiles._left] extends = "_base" [tool.datamodel-codegen.profiles._right] extends = "_base" field-constraints = true [tool.datamodel-codegen.profiles.api] extends = ["_left", "_right"] """ (tmp_path / "pyproject.toml").write_text(pyproject_toml) # ... test assertions
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/datamodel_code_generator/__main__.pytests/test_main_kr.py
🧰 Additional context used
🧬 Code graph analysis (2)
src/datamodel_code_generator/__main__.py (1)
src/datamodel_code_generator/__init__.py (1)
Error(297-306)
tests/test_main_kr.py (4)
tests/conftest.py (1)
freeze_time(313-316)tests/main/conftest.py (1)
run_main_with_args(215-241)src/datamodel_code_generator/__init__.py (1)
chdir(245-255)src/datamodel_code_generator/__main__.py (2)
main(1055-1352)Exit(123-129)
⏰ 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). (10)
- GitHub Check: Analyze (python)
- GitHub Check: 3.13 on Windows
- GitHub Check: 3.11 on Ubuntu
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.12 on Windows
- GitHub Check: py312-black23 on Ubuntu
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.10 on macOS
- GitHub Check: benchmarks
🔇 Additional comments (7)
src/datamodel_code_generator/__main__.py (2)
702-739: Well-structured profile inheritance implementation.The function correctly handles:
- Circular reference detection with proper chain reporting
- Both single string and list-based multiple inheritance
- Self-extension check
- Proper shallow merge semantics where child overrides parent
One minor observation: the
visited.copy()on line 735 creates a new set for each parent branch, which is correct for detecting circular references within each inheritance path but means a diamond inheritance pattern (A extends B, A extends C, both B and C extend D) would resolve D twice. This is acceptable behavior for shallow merge semantics.
759-760: Clean integration of profile resolution.The resolved profile correctly merges inherited settings before updating
base_config, maintaining the proper precedence: base config < parent profiles < child profile.tests/test_main_kr.py (5)
1832-1865: Good test for single parent inheritance.The test correctly validates that a profile extending
_baseinherits thesnake-case-field = truesetting, producingfirst_namein the output.
1867-1904: Comprehensive multiple inheritance test.The test validates that extending
["_snake", "_constraints"]merges settings from both parent profiles. The assertions verify bothfirst_name(from_snake) andmin_length(from_constraints) appear in output.
1906-1944: Inheritance chain test is well-designed.Validates that
api -> _middle -> _basecorrectly inheritssnake-case-fieldfrom_baseandfield-constraintsfrom_middle.
1946-2022: Error case tests provide good coverage.The three error scenarios are well-tested:
- Circular extends (a→b→a): validates "Circular extends detected" message
- Not found extends: validates "Extended profile 'nonexistent' not found" message
- Self-extension: validates "cannot extend itself" message
2024-2058: Child override test validates correct merge semantics.Correctly tests that when
apiextends_basebut overridessnake-case-field = false, the child's setting takes precedence, resulting infirstName(notfirst_name) in the output.
CodSpeed Performance ReportMerging #2834 will not alter performanceComparing
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2834 +/- ##
=======================================
Coverage 99.52% 99.52%
=======================================
Files 90 90
Lines 13999 14092 +93
Branches 1668 1674 +6
=======================================
+ Hits 13932 14025 +93
Misses 36 36
Partials 31 31
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:
|
c331e42 to
0d3f3f0
Compare
0d3f3f0 to
27b4f64
Compare
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR adds a new optional feature (profile inheritance via
The changes are purely additive - they enable profile inheritance without modifying any existing functionality or requiring changes to existing configurations. 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.