Skip to content

Refactor generate() and Parser to use config directly#2878

Merged
koxudaxi merged 3 commits intomainfrom
refactor/use-config-directly
Dec 30, 2025
Merged

Refactor generate() and Parser to use config directly#2878
koxudaxi merged 3 commits intomainfrom
refactor/use-config-directly

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 30, 2025

Summary by CodeRabbit

  • Refactor
    • Restructured internal configuration system to enhance code organization and maintainability.
    • Improved parser architecture with enhanced type safety and structural flexibility for future extensibility.

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 30, 2025

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 22 minutes and 8 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 180595a and 8a4e90d.

📒 Files selected for processing (14)
  • .pre-commit-config.yaml
  • pyproject.toml
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/_types/__init__.py
  • src/datamodel_code_generator/_types/graphql_parser_config_dict.py
  • src/datamodel_code_generator/_types/jsonschema_parser_config_dict.py
  • src/datamodel_code_generator/_types/openapi_parser_config_dict.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/graphql.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • src/datamodel_code_generator/parser/openapi.py
  • tests/main/test_main_general.py
  • tox.ini
📝 Walkthrough

Walkthrough

The PR refactors configuration handling across the codebase. The main module now routes all option access through GenerateConfig objects instead of local variables. The Parser base class becomes generic with a ParserConfigT type variable, introduces a _create_default_config classmethod, and updates init to accept generic config types. Derived parsers inherit with concrete types.

Changes

Cohort / File(s) Summary
Configuration routing refactor
src/datamodel_code_generator/__init__.py
Replaced 50+ direct local variable reads (output, target_python_version, encoding, feature flags) with config.* access patterns. Updated payload construction for OpenAPI/GraphQL/JSONSchema parsers, header generation logic, file I/O operations, and DataclassArguments construction to consume from GenerateConfig object throughout multiple call sites.
Generic Parser base class
src/datamodel_code_generator/parser/base.py
Made Parser generic with ParserConfigT type variable bound to ParserConfig. Added _create_default_config classmethod for unified config construction. Updated init signature from config: ParserConfig | None to config: ParserConfigT | None and refactored initialization flow to populate internal state from resolved config object. Added Generic, TypeVar imports and type annotations.
Parser subclass generic inheritance
src/datamodel_code_generator/parser/graphql.py, src/datamodel_code_generator/parser/jsonschema.py
Updated class signatures to inherit from Parser["ParserConfig"] instead of non-generic Parser base. No functional logic changes.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed, breaking-change

Poem

🐰 Configuration now flows through GenConfig's gentle stream,
Parser becomes generic—a type-safe dream!
No more scattered variables across the land,
One config object guides each parsing hand. ✨

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 accurately summarizes the main changes: refactoring the generate() function and Parser class to rely on config objects instead of scattered parameters.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.

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 30, 2025

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

Comment thread src/datamodel_code_generator/__init__.py Fixed
Comment thread src/datamodel_code_generator/parser/base.py Dismissed
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2878 will not alter performance

Comparing refactor/use-config-directly (8a4e90d) with main (0c5d538)

⚠️ 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 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (0c5d538) to head (8a4e90d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2878      +/-   ##
==========================================
- Coverage   99.39%   99.38%   -0.01%     
==========================================
  Files          91       91              
  Lines       15820    15648     -172     
  Branches     1845     1849       +4     
==========================================
- Hits        15724    15552     -172     
  Misses         50       50              
  Partials       46       46              
Flag Coverage Δ
unittests 99.38% <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.

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 (3)
src/datamodel_code_generator/__init__.py (1)

616-639: Consistent use of config.encoding for file operations.

All file read operations correctly use config.encoding for consistent character encoding handling.

Regarding the static analysis hint at line 619: the # noqa: PLC0415 directive for the import io statement appears intentional for lazy imports. If PLC0415 is not enabled in your Ruff configuration, you could remove the directive.

src/datamodel_code_generator/parser/base.py (2)

704-706: Remove unused noqa directives.

The # noqa: PLC0415 directives are unnecessary as this rule is not enabled in the project's linter configuration. As per the static analysis hints, these can be safely removed.

🔎 Proposed fix
-        from datamodel_code_generator import types as types_module  # noqa: PLC0415
-        from datamodel_code_generator.config import ParserConfig  # noqa: PLC0415
-        from datamodel_code_generator.model import base as model_base  # noqa: PLC0415
+        from datamodel_code_generator import types as types_module
+        from datamodel_code_generator.config import ParserConfig
+        from datamodel_code_generator.model import base as model_base

728-728: Remove unused noqa directive.

The # noqa: PLR0912, PLR0915 directive is flagged as unused. The refactoring may have simplified the function enough that these complexity rules no longer trigger.

🔎 Proposed fix
-    def __init__(  # noqa: PLR0912, PLR0915
+    def __init__(
📜 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 0c5d538 and 180595a.

📒 Files selected for processing (4)
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/graphql.py
  • src/datamodel_code_generator/parser/jsonschema.py
🧰 Additional context used
🧬 Code graph analysis (1)
src/datamodel_code_generator/parser/jsonschema.py (1)
src/datamodel_code_generator/parser/base.py (1)
  • Parser (691-3127)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/__init__.py

619-619: Unused noqa directive (non-enabled: PLC0415)

Remove unused noqa directive

(RUF100)

src/datamodel_code_generator/parser/base.py

704-704: Unused noqa directive (non-enabled: PLC0415)

Remove unused noqa directive

(RUF100)


705-705: Unused noqa directive (non-enabled: PLC0415)

Remove unused noqa directive

(RUF100)


706-706: Unused noqa directive (non-enabled: PLC0415)

Remove unused noqa directive

(RUF100)


728-728: Unused noqa directive (non-enabled: PLR0912, PLR0915)

Remove unused noqa directive

(RUF100)

⏰ 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). (12)
  • GitHub Check: 3.10 on Ubuntu
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.10 on Windows
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: 3.14 on macOS
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: py312-black24 on Ubuntu
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
  • GitHub Check: benchmarks
🔇 Additional comments (11)
src/datamodel_code_generator/parser/graphql.py (1)

66-66: LGTM!

The generic type parameterization Parser["ParserConfig"] correctly aligns with the base Parser class becoming generic. The forward reference string is appropriate since ParserConfig is imported under TYPE_CHECKING.

src/datamodel_code_generator/parser/jsonschema.py (1)

526-526: LGTM!

Consistent with the GraphQLParser change. The generic type parameterization correctly specifies ParserConfig as the config type for this parser.

src/datamodel_code_generator/__init__.py (3)

496-531: LGTM!

The refactor correctly extracts mutable configuration values to local variables where they may be modified during processing (e.g., input_file_type, custom_file_header), while accessing immutable config fields directly. The HTTP handling and dataclass arguments initialization are consistent.


689-807: LGTM - Parser instantiation refactored to use config directly.

The extensive refactor to use config.* fields for parser instantiation is consistent and thorough. The conditional logic for enum_field_as_literal (lines 728-730) and set_default_enum_member (lines 735-737) correctly preserves the original behavior while accessing values from config.


810-941: LGTM - Output handling and formatting refactored to use config directly.

The parse() call, header generation, file writing, and formatter setup all correctly use config.* fields. The encoding is consistently applied via config.encoding for both reading and writing operations.

src/datamodel_code_generator/parser/base.py (6)

19-30: LGTM!

The updated typing imports correctly support the new generic Parser[ParserConfigT] class pattern.


93-94: LGTM!

The type variable is correctly defined with a forward reference bound to ParserConfig, enabling subclasses to specify their own config types while maintaining type safety.


691-691: LGTM!

The class correctly combines abstract base class functionality with generic typing.


745-750: LGTM!

Good defensive programming with a clear error message when both config and **options are provided. The fallback to _create_default_config maintains backward compatibility for callers using keyword arguments.


752-936: LGTM!

The configuration assignments are cleanly migrated to use the config object. The or [], or {}, or False patterns appropriately handle optional config values that may be None.


698-726: This concern is not applicable. All fields in ParserConfig have explicit defaults (either None or specific values like False, pydantic_model.BaseModel, etc.), so there are no required fields without defaults. The code safely handles Pydantic v1 by collecting all field defaults and updating them with provided options before calling construct(), ensuring all fields always have values.

Likely an incorrect or invalid review comment.

Comment thread src/datamodel_code_generator/__init__.py Dismissed
@koxudaxi koxudaxi force-pushed the refactor/use-config-directly branch 2 times, most recently from 381fd2c to 69f301d Compare December 30, 2025 22:00
Comment thread src/datamodel_code_generator/config.py Dismissed
Comment thread src/datamodel_code_generator/config.py Dismissed
Comment thread src/datamodel_code_generator/config.py Dismissed
Comment thread src/datamodel_code_generator/config.py Dismissed
Comment thread src/datamodel_code_generator/parser/base.py Fixed
@koxudaxi koxudaxi force-pushed the refactor/use-config-directly branch from 69f301d to 1b202eb Compare December 30, 2025 22:05
- Create GraphQLParserConfig, JSONSchemaParserConfig, and OpenAPIParserConfig
- OpenAPIParserConfig inherits from JSONSchemaParserConfig (which inherits from ParserConfig)
- GraphQLParserConfig inherits from ParserConfig
- Move OpenAPI-specific options (openapi_scopes, include_path_parameters, use_status_code_in_response_name) to OpenAPIParserConfig
- Move GraphQL-specific options (data_model_scalar_type, data_model_union_type) to GraphQLParserConfig
- Make Parser._create_default_config() abstract method
- Each parser now implements its own _create_default_config() method
- Store config object on parser instance (self.config)
- Generate TypedDict types for all new config classes
- Update tests to use specialized config types
@koxudaxi koxudaxi force-pushed the refactor/use-config-directly branch from 1b202eb to 8a4e90d Compare December 30, 2025 22:07
@koxudaxi koxudaxi merged commit 9ef026e into main Dec 30, 2025
38 checks passed
@koxudaxi koxudaxi deleted the refactor/use-config-directly branch December 30, 2025 22:11
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is an internal refactoring that restructures how configuration is passed to parsers. While the constructor signatures have changed (OpenAPIParser and GraphQLParser no longer have explicit keyword arguments for parser-specific options like openapi_scopes or data_model_scalar_type), these parameters are still accepted via **kwargs since they're included in the new specialized TypedDict types (OpenAPIParserConfigDict, GraphQLParserConfigDict). At runtime, existing code will continue to work. The changes primarily affect type checking - users with strict type checkers will see warnings if they pass ParserConfig instead of the new specialized config types (OpenAPIParserConfig, GraphQLParserConfig, JSONSchemaParserConfig). The generate() function signature and CLI remain unchanged. No changes to generated code output, templates, default behaviors, or error handling. This is internal API restructuring for better code organization and type safety, not a breaking change for end users.


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.

2 participants