|
80 | 80 | T = TypeVar("T") |
81 | 81 | _ConfigT = TypeVar("_ConfigT", bound="ParserConfig") |
82 | 82 |
|
83 | | -# Import is_pydantic_v2 here for module-level YamlValue type definition |
84 | | -from datamodel_code_generator.util import is_pydantic_v2 # noqa: E402 |
85 | | - |
86 | 83 | if not TYPE_CHECKING: # pragma: no branch |
87 | 84 | YamlScalar: TypeAlias = str | int | float | bool | None |
88 | | - if is_pydantic_v2(): |
89 | | - YamlValue = TypeAliasType("YamlValue", "dict[str, YamlValue] | list[YamlValue] | YamlScalar") |
90 | | - else: |
91 | | - # Pydantic v1 cannot handle TypeAliasType, use Any for recursive parts |
92 | | - YamlValue: TypeAlias = dict[str, Any] | list[Any] | YamlScalar |
| 85 | + YamlValue = TypeAliasType("YamlValue", "dict[str, YamlValue] | list[YamlValue] | YamlScalar") |
93 | 86 |
|
94 | 87 |
|
95 | 88 | GeneratedModules: TypeAlias = dict[tuple[str, ...], str] |
@@ -471,19 +464,13 @@ def _create_parser_config( |
471 | 464 | Filters GenerateConfig fields to only those expected by the parser config class, |
472 | 465 | then merges with additional_options. |
473 | 466 | """ |
474 | | - if is_pydantic_v2(): |
475 | | - parser_config_fields = set(config_class.model_fields.keys()) |
476 | | - all_options = { |
477 | | - k: v |
478 | | - for k, v in generate_config.model_dump().items() |
479 | | - if k in parser_config_fields and k not in additional_options |
480 | | - } | dict(additional_options) |
481 | | - return config_class.model_validate(all_options) |
482 | | - parser_config_fields = set(config_class.__fields__.keys()) |
| 467 | + parser_config_fields = set(config_class.model_fields.keys()) |
483 | 468 | all_options = { |
484 | | - k: v for k, v in generate_config.dict().items() if k in parser_config_fields and k not in additional_options |
| 469 | + k: v |
| 470 | + for k, v in generate_config.model_dump().items() |
| 471 | + if k in parser_config_fields and k not in additional_options |
485 | 472 | } | dict(additional_options) |
486 | | - return config_class.parse_obj(all_options) |
| 473 | + return config_class.model_validate(all_options) |
487 | 474 |
|
488 | 475 |
|
489 | 476 | def generate( # noqa: PLR0912, PLR0914, PLR0915 |
@@ -518,18 +505,11 @@ def generate( # noqa: PLR0912, PLR0914, PLR0915 |
518 | 505 | raise ValueError(msg) |
519 | 506 |
|
520 | 507 | if config is None: |
521 | | - if is_pydantic_v2(): |
522 | | - from datamodel_code_generator.model.pydantic_v2 import UnionMode # noqa: PLC0415 |
523 | | - from datamodel_code_generator.types import StrictTypes # noqa: PLC0415 |
| 508 | + from datamodel_code_generator.model.pydantic_v2 import UnionMode # noqa: PLC0415 |
| 509 | + from datamodel_code_generator.types import StrictTypes # noqa: PLC0415 |
524 | 510 |
|
525 | | - GenerateConfig.model_rebuild(_types_namespace={"StrictTypes": StrictTypes, "UnionMode": UnionMode}) |
526 | | - config = GenerateConfig.model_validate(options) |
527 | | - else: |
528 | | - from datamodel_code_generator.enums import UnionMode # noqa: PLC0415 |
529 | | - from datamodel_code_generator.types import StrictTypes # noqa: PLC0415 |
530 | | - |
531 | | - GenerateConfig.update_forward_refs(StrictTypes=StrictTypes, UnionMode=UnionMode) |
532 | | - config = GenerateConfig(**options) |
| 511 | + GenerateConfig.model_rebuild(_types_namespace={"StrictTypes": StrictTypes, "UnionMode": UnionMode}) |
| 512 | + config = GenerateConfig.model_validate(options) |
533 | 513 |
|
534 | 514 | # Variables that may be modified during processing |
535 | 515 | input_filename = config.input_filename |
@@ -979,13 +959,6 @@ def __getattr__(name: str) -> Any: |
979 | 959 | if name in _LAZY_IMPORTS: |
980 | 960 | import importlib # noqa: PLC0415 |
981 | 961 |
|
982 | | - if name == "GenerateConfig" and not is_pydantic_v2(): # pragma: no cover |
983 | | - msg = ( |
984 | | - f"'{name}' is only available in Pydantic v2 environments. " |
985 | | - "Use 'from datamodel_code_generator.config import GenerateConfig' instead." |
986 | | - ) |
987 | | - raise ImportError(msg) |
988 | | - |
989 | 962 | module = importlib.import_module(_LAZY_IMPORTS[name]) |
990 | 963 | return getattr(module, name) |
991 | 964 | msg = f"module {__name__!r} has no attribute {name!r}" |
@@ -1034,5 +1007,4 @@ def __getattr__(name: str) -> Any: |
1034 | 1007 | "generate_dynamic_models", # noqa: F822 |
1035 | 1008 | ] |
1036 | 1009 |
|
1037 | | -if is_pydantic_v2(): # pragma: no cover |
1038 | | - __all__ += ["GenerateConfig"] |
| 1010 | +__all__ += ["GenerateConfig"] |
0 commit comments