-
-
Notifications
You must be signed in to change notification settings - Fork 437
Add config parameter to Parser classes #2863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ef6f61c
4be3196
8f4aff3
2896b19
a6e64fc
97bf992
b0bacf7
a497759
507c33f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||||||||||||
| from typing import TYPE_CHECKING, Annotated, Any | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from pydantic import BaseModel, Field | ||||||||||||||||||||||
| from typing_extensions import Self | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from datamodel_code_generator.enums import ( | ||||||||||||||||||||||
| DEFAULT_SHARED_MODULE_NAME, | ||||||||||||||||||||||
|
|
@@ -85,7 +86,7 @@ class Config: | |||||||||||||||||||||
| field_constraints: bool = False | ||||||||||||||||||||||
| snake_case_field: bool = False | ||||||||||||||||||||||
| strip_default_none: bool = False | ||||||||||||||||||||||
| aliases: Mapping[str, str] | None = None | ||||||||||||||||||||||
| aliases: Mapping[str, str | list[str]] | None = None | ||||||||||||||||||||||
| disable_timestamp: bool = False | ||||||||||||||||||||||
| enable_version_header: bool = False | ||||||||||||||||||||||
| enable_command_header: bool = False | ||||||||||||||||||||||
|
|
@@ -201,9 +202,36 @@ class ParserConfig(BaseModel): | |||||||||||||||||||||
| class Config: | ||||||||||||||||||||||
| """Pydantic v1 model config.""" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| extra = "forbid" | ||||||||||||||||||||||
| extra = "allow" | ||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Inconsistent Pydantic v2 config (line 199) has
This inconsistency undermines cross-version compatibility and could mask typos or incorrect field names when running on v1. 🔎 Proposed fix- extra = "allow"
+ extra = "forbid"If extra fields are needed for the config-merging pattern described in the PR, consider handling them explicitly in 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| arbitrary_types_allowed = True | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||
| def from_options(cls, options: dict[str, Any]) -> Self: | ||||||||||||||||||||||
| """Create a ParserConfig from options dict with version-compatible validation.""" | ||||||||||||||||||||||
| if is_pydantic_v2(): | ||||||||||||||||||||||
| from datamodel_code_generator.model.base import ( # noqa: PLC0415 | ||||||||||||||||||||||
| DataModel, | ||||||||||||||||||||||
| DataModelFieldBase, | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| from datamodel_code_generator.types import DataTypeManager, StrictTypes # noqa: PLC0415 | ||||||||||||||||||||||
|
Comment on lines
+212
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused The 🔎 Proposed fix- from datamodel_code_generator.model.base import ( # noqa: PLC0415
+ from datamodel_code_generator.model.base import (
DataModel,
DataModelFieldBase,
)
- from datamodel_code_generator.types import DataTypeManager, StrictTypes # noqa: PLC0415
+ from datamodel_code_generator.types import DataTypeManager, StrictTypes📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.14.10)212-212: Unused Remove unused (RUF100) 216-216: Unused Remove unused (RUF100) 🤖 Prompt for AI Agents |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| cls.model_rebuild( | ||||||||||||||||||||||
| _types_namespace={ | ||||||||||||||||||||||
| "DataModel": DataModel, | ||||||||||||||||||||||
| "DataModelFieldBase": DataModelFieldBase, | ||||||||||||||||||||||
| "DataTypeManager": DataTypeManager, | ||||||||||||||||||||||
| "StrictTypes": StrictTypes, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| return cls.model_validate(options) | ||||||||||||||||||||||
| # For Pydantic v1, use construct() to skip validation (forward refs not resolved) | ||||||||||||||||||||||
| defaults: dict[str, Any] = {} | ||||||||||||||||||||||
| for field_name, field in cls.__fields__.items(): # type: ignore[attr-defined] | ||||||||||||||||||||||
| # Note: ParserConfig fields don't use default_factory, so we only handle static defaults | ||||||||||||||||||||||
| defaults[field_name] = field.default | ||||||||||||||||||||||
| defaults.update(options) | ||||||||||||||||||||||
| return cls.construct(**defaults) # type: ignore[return-value] | ||||||||||||||||||||||
|
Comment on lines
+227
to
+233
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pydantic v1 path bypasses validation entirely using The Pydantic v1 code path uses Consider alternative approaches:
🤖 Prompt for AI Agents |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| data_model_type: type[DataModel] = pydantic_model.BaseModel | ||||||||||||||||||||||
| data_model_root_type: type[DataModel] = pydantic_model.CustomRootType | ||||||||||||||||||||||
| data_type_manager_type: type[DataTypeManager] = pydantic_model.DataTypeManager | ||||||||||||||||||||||
|
|
@@ -220,7 +248,7 @@ class Config: | |||||||||||||||||||||
| field_constraints: bool = False | ||||||||||||||||||||||
| snake_case_field: bool = False | ||||||||||||||||||||||
| strip_default_none: bool = False | ||||||||||||||||||||||
| aliases: Mapping[str, str] | None = None | ||||||||||||||||||||||
| aliases: Mapping[str, str | list[str]] | None = None | ||||||||||||||||||||||
| allow_population_by_field_name: bool = False | ||||||||||||||||||||||
| apply_default_values_for_required_fields: bool = False | ||||||||||||||||||||||
| allow_extra_fields: bool = False | ||||||||||||||||||||||
|
|
@@ -316,6 +344,21 @@ class Config: | |||||||||||||||||||||
| target_pydantic_version: TargetPydanticVersion | None = None | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class OpenAPIParserConfig(ParserConfig): | ||||||||||||||||||||||
| """Configuration model for OpenAPIParser.__init__().""" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| openapi_scopes: list[OpenAPIScope] | None = None | ||||||||||||||||||||||
| include_path_parameters: bool = False | ||||||||||||||||||||||
| use_status_code_in_response_name: bool = False | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class GraphQLParserConfig(ParserConfig): | ||||||||||||||||||||||
| """Configuration model for GraphQLParser.__init__().""" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| data_model_scalar_type: type[DataModel] | None = None | ||||||||||||||||||||||
| data_model_union_type: type[DataModel] | None = None | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| class ParseConfig(BaseModel): | ||||||||||||||||||||||
| """Configuration model for Parser.parse().""" | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: koxudaxi/datamodel-code-generator
Length of output: 17256
🏁 Script executed:
Repository: koxudaxi/datamodel-code-generator
Length of output: 2632
🏁 Script executed:
Repository: koxudaxi/datamodel-code-generator
Length of output: 1141
Fix type signature mismatch in
watch.pyforaliasesparameter.The
watch_and_regeneratefunction declaresaliases: dict[str, str] | None(line 26) but should bedict[str, str | list[str]] | Noneto match the updated signature ingenerate()and the configuration classes. The type must support bothstrandlist[str]values to maintain consistency with the extended type inGenerateConfigandParserConfig.🤖 Prompt for AI Agents