Skip to content

Add extends support for profile inheritance#2834

Merged
koxudaxi merged 1 commit intomainfrom
feature/profile-extends
Dec 28, 2025
Merged

Add extends support for profile inheritance#2834
koxudaxi merged 1 commit intomainfrom
feature/profile-extends

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 28, 2025

Summary by CodeRabbit

  • New Features

    • Profiles now support inheritance via an extends mechanism with validation for circular, missing, and self-extends and proper override resolution.
  • Tests

    • Added comprehensive tests for single/multiple/chain inheritance, overrides, and error scenarios with new expected outputs.
    • Added test utilities for asserting error messages, validating generated code, and warming heavy-module imports.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 28, 2025

📝 Walkthrough

Walkthrough

Adds profile inheritance resolution for pyproject.toml profiles via a new _resolve_profile_extends helper with circular/self-checks; config loading now applies resolved profiles. Tests and test helpers were added/extended to validate inheritance cases, errors, and generated outputs.

Changes

Cohort / File(s) Summary
Profile inheritance implementation
src/datamodel_code_generator/__main__.py
Adds _resolve_profile_extends(profiles, profile_name, visited=None) to recursively resolve extends (supports list or single parent), detects circular/self-extends and missing profiles; _get_pyproject_toml_config() now applies the resolved profile rather than the raw profile dict.
CLI/profile tests
tests/test_main_kr.py
Adds tests covering single-parent, multiple-parents, inheritance chains, circular/self-extend errors, missing parent error, and child-overrides; uses temp pyproject.toml fixtures and asserts generated outputs and error messages.
Test utilities & validation
tests/conftest.py
Adds assert_error_message(capsys, expected), min_version and _preload_heavy_modules fixtures, and validate_generated_code() helper; extends snapshot/file format aliases.
Expected outputs for profiles
tests/data/expected/main_kr/pyproject_profile/*
tests/data/expected/main_kr/pyproject_profile/extends_single.py, .../extends_multiple.py, .../extends_chain.py, .../extends_override.py
New expected generated Python modules for different profile inheritance scenarios (single, multiple, chain, override) used by the new tests.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐇 I nibbled at configs, found lines to mend,

"Extend thy parent!" I politely penned.
Chains now resolve and circulars I fence,
Tests hop along — precise and concise.
A rabbit's small stamp on profiles hence.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add extends support for profile inheritance' directly and clearly describes the main feature addition - implementing profile inheritance via extends mechanism.
Docstring Coverage ✅ Passed Docstring coverage is 92.86% which is sufficient. The required threshold is 80.00%.
✨ 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 feature/profile-extends

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c331e42 and 27b4f64.

📒 Files selected for processing (7)
  • src/datamodel_code_generator/__main__.py
  • tests/conftest.py
  • tests/data/expected/main_kr/pyproject_profile/extends_chain.py
  • tests/data/expected/main_kr/pyproject_profile/extends_multiple.py
  • tests/data/expected/main_kr/pyproject_profile/extends_override.py
  • tests/data/expected/main_kr/pyproject_profile/extends_single.py
  • tests/test_main_kr.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/datamodel_code_generator/main.py
🧰 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_chain.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_override.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_single.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_override.py (3)
tests/data/expected/main_kr/pyproject_profile/extends_chain.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_multiple.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_single.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_single.py (3)
tests/data/expected/main_kr/pyproject_profile/extends_chain.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_multiple.py (1)
  • Model (9-10)
tests/data/expected/main_kr/pyproject_profile/extends_override.py (1)
  • Model (9-10)
tests/test_main_kr.py (4)
tests/conftest.py (2)
  • assert_error_message (604-620)
  • create_assert_file_content (451-502)
tests/main/conftest.py (3)
  • output_file (98-100)
  • run_main_and_assert (244-408)
  • run_main_with_args (215-241)
src/datamodel_code_generator/__init__.py (1)
  • chdir (245-255)
src/datamodel_code_generator/__main__.py (1)
  • 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). (15)
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: 3.14 on macOS
  • GitHub Check: 3.10 on macOS
  • GitHub Check: 3.12 on Windows
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: py312-black24 on Ubuntu
  • GitHub Check: 3.13 on Windows
  • GitHub Check: py312-pydantic1 on Ubuntu
  • GitHub Check: 3.13 on Ubuntu
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks
🔇 Additional comments (10)
tests/data/expected/main_kr/pyproject_profile/extends_chain.py (1)

1-10: LGTM! Valid test fixture for chained profile inheritance.

This expected output file correctly validates that chained extends (_base → _middle → api) properly accumulates settings (snake-case-field + field-constraints).

tests/data/expected/main_kr/pyproject_profile/extends_multiple.py (1)

1-10: LGTM! Valid test fixture for multiple parent inheritance.

This expected output correctly validates that multiple extends (["_snake", "_constraints"]) merges settings from both parents.

tests/data/expected/main_kr/pyproject_profile/extends_override.py (1)

1-10: LGTM! Valid test fixture for override behavior.

This expected output correctly validates that child profile settings override parent settings (snake-case-field explicitly set to false overrides parent's true).

tests/data/expected/main_kr/pyproject_profile/extends_single.py (1)

1-10: LGTM! Valid test fixture for single parent inheritance.

This expected output correctly validates that a profile inheriting from a single parent (_base) receives the parent's settings (snake-case-field).

tests/test_main_kr.py (2)

18-18: LGTM! Correct import for new test helper.

The import of assert_error_message from conftest is properly used in the error-handling tests below.


1832-2026: LGTM! Comprehensive test coverage for profile inheritance.

This test suite thoroughly validates the new extends feature with excellent coverage:

Success cases:

  • Single parent inheritance
  • Multiple parent inheritance (list syntax)
  • Chained inheritance (transitive)
  • Child overriding parent settings

Error cases:

  • Circular reference detection
  • Missing parent profile
  • Self-reference detection

All tests follow consistent patterns, use appropriate fixtures, and validate both generated output and error messages.

tests/conftest.py (4)

604-620: LGTM! Clean and focused error assertion helper.

The assert_error_message function provides a convenient way to validate stderr output in error test cases. Good use of __tracebackhide__ for cleaner test output.


630-633: LGTM! Convenient fixture for version strings.

The min_version fixture provides a consistent way to format the minimum Python version for tests.


636-648: LGTM! Smart performance optimization.

Pre-loading heavy modules at session start reduces per-test import overhead, especially beneficial when running tests in parallel with pytest-xdist. The docstring clearly explains the rationale.


650-677: LGTM! Well-designed code validation helper.

The validate_generated_code function provides a systematic way to verify generated code with timing statistics and error tracking. Good separation of compile and exec phases with the do_exec parameter.


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

github-actions Bot commented Dec 28, 2025

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 45fac78 and c331e42.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/__main__.py
  • tests/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 _base inherits the snake-case-field = true setting, producing first_name in the output.


1867-1904: Comprehensive multiple inheritance test.

The test validates that extending ["_snake", "_constraints"] merges settings from both parent profiles. The assertions verify both first_name (from _snake) and min_length (from _constraints) appear in output.


1906-1944: Inheritance chain test is well-designed.

Validates that api -> _middle -> _base correctly inherits snake-case-field from _base and field-constraints from _middle.


1946-2022: Error case tests provide good coverage.

The three error scenarios are well-tested:

  1. Circular extends (a→b→a): validates "Circular extends detected" message
  2. Not found extends: validates "Extended profile 'nonexistent' not found" message
  3. Self-extension: validates "cannot extend itself" message

2024-2058: Child override test validates correct merge semantics.

Correctly tests that when api extends _base but overrides snake-case-field = false, the child's setting takes precedence, resulting in firstName (not first_name) in the output.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 28, 2025

CodSpeed Performance Report

Merging #2834 will not alter performance

Comparing feature/profile-extends (27b4f64) with main (0f06a7d)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 11 untouched
⏩ 98 skipped1

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.52%. Comparing base (45fac78) to head (27b4f64).
⚠️ Report is 2 commits behind head on main.

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           
Flag Coverage Δ
unittests 99.52% <100.00%> (+<0.01%) ⬆️

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.

@koxudaxi koxudaxi force-pushed the feature/profile-extends branch from c331e42 to 0d3f3f0 Compare December 28, 2025 13:07
@koxudaxi koxudaxi force-pushed the feature/profile-extends branch from 0d3f3f0 to 27b4f64 Compare December 28, 2025 13:12
@koxudaxi koxudaxi enabled auto-merge (squash) December 28, 2025 13:14
@koxudaxi koxudaxi merged commit e4394af into main Dec 28, 2025
34 checks passed
@koxudaxi koxudaxi deleted the feature/profile-extends branch December 28, 2025 13:15
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR adds a new optional feature (profile inheritance via extends key) without changing any existing behavior. Analysis:

  1. Code Generation Changes: No changes to generated code output. The new feature only affects how configuration is resolved from pyproject.toml profiles.

  2. Custom Template Update Required: No template changes. The PR only modifies the __main__.py file's configuration resolution logic.

  3. API/CLI Changes: No new CLI options or API changes. The extends key is an optional new field within existing profile sections in pyproject.toml.

  4. Default Behavior Changes: Existing profiles without extends work exactly as before. The _resolve_profile_extends() function returns dict(profile.items()) when no extends is present, preserving existing behavior.

  5. Python Version Changes: No Python version requirements changed.

  6. Error Handling Changes: New error messages are added for invalid extends configurations (circular, missing, self-reference), but these only affect users who use the new extends feature - existing configurations are unaffected.

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

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 1, 2026

🎉 Released in 0.51.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.

1 participant