Refactor generate() and Parser to use config directly#2878
Conversation
|
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 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. 📒 Files selected for processing (14)
📝 WalkthroughWalkthroughThe 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
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
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-2878.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging #2878 will not alter performanceComparing
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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
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:
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/datamodel_code_generator/__init__.py (1)
616-639: Consistent use ofconfig.encodingfor file operations.All file read operations correctly use
config.encodingfor consistent character encoding handling.Regarding the static analysis hint at line 619: the
# noqa: PLC0415directive for theimport iostatement 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 unusednoqadirectives.The
# noqa: PLC0415directives 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 unusednoqadirective.The
# noqa: PLR0912, PLR0915directive 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
📒 Files selected for processing (4)
src/datamodel_code_generator/__init__.pysrc/datamodel_code_generator/parser/base.pysrc/datamodel_code_generator/parser/graphql.pysrc/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 baseParserclass becoming generic. The forward reference string is appropriate sinceParserConfigis imported underTYPE_CHECKING.src/datamodel_code_generator/parser/jsonschema.py (1)
526-526: LGTM!Consistent with the
GraphQLParserchange. The generic type parameterization correctly specifiesParserConfigas 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 forenum_field_as_literal(lines 728-730) andset_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 useconfig.*fields. The encoding is consistently applied viaconfig.encodingfor 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
configand**optionsare provided. The fallback to_create_default_configmaintains backward compatibility for callers using keyword arguments.
752-936: LGTM!The configuration assignments are cleanly migrated to use the
configobject. Theor [],or {},or Falsepatterns appropriately handle optional config values that may beNone.
698-726: This concern is not applicable. All fields in ParserConfig have explicit defaults (eitherNoneor specific values likeFalse,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 callingconstruct(), ensuring all fields always have values.Likely an incorrect or invalid review comment.
381fd2c to
69f301d
Compare
69f301d to
1b202eb
Compare
- 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
1b202eb to
8a4e90d
Compare
Breaking Change AnalysisResult: 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 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
✏️ Tip: You can customize this high-level summary in your review settings.