Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ jobs:
name: py312-isort6
- tox_env: py312-isort5-parallel
name: py312-isort5
- tox_env: py312-pydantic1-parallel
name: py312-pydantic1
runs-on: ${{ matrix.os == '' && 'ubuntu-24.04' || matrix.os }}
env:
OS: ${{ matrix.os == '' && 'ubuntu-24.04' || matrix.os}}
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ skip = '.git,*.lock,tests,docs/cli-reference,CHANGELOG.md,docs/changelog.md,docs
[tool.pytest.ini_options]
filterwarnings = [
"error",
"ignore:^.*Pydantic v1 runtime support is deprecated.*:DeprecationWarning",
"ignore:^.*No --output-model-type specified.*:DeprecationWarning",
"ignore:^.*Pydantic v2 with --use-annotated is recommended.*:DeprecationWarning",
"ignore:^.*`--validation` option is deprecated.*",
Expand Down
50 changes: 11 additions & 39 deletions src/datamodel_code_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,9 @@
T = TypeVar("T")
_ConfigT = TypeVar("_ConfigT", bound="ParserConfig")

# Import is_pydantic_v2 here for module-level YamlValue type definition
from datamodel_code_generator.util import is_pydantic_v2 # noqa: E402

if not TYPE_CHECKING: # pragma: no branch
YamlScalar: TypeAlias = str | int | float | bool | None
if is_pydantic_v2():
YamlValue = TypeAliasType("YamlValue", "dict[str, YamlValue] | list[YamlValue] | YamlScalar")
else:
# Pydantic v1 cannot handle TypeAliasType, use Any for recursive parts
YamlValue: TypeAlias = dict[str, Any] | list[Any] | YamlScalar
YamlValue = TypeAliasType("YamlValue", "dict[str, YamlValue] | list[YamlValue] | YamlScalar")


GeneratedModules: TypeAlias = dict[tuple[str, ...], str]
Expand Down Expand Up @@ -471,19 +464,13 @@ def _create_parser_config(
Filters GenerateConfig fields to only those expected by the parser config class,
then merges with additional_options.
"""
if is_pydantic_v2():
parser_config_fields = set(config_class.model_fields.keys())
all_options = {
k: v
for k, v in generate_config.model_dump().items()
if k in parser_config_fields and k not in additional_options
} | dict(additional_options)
return config_class.model_validate(all_options)
parser_config_fields = set(config_class.__fields__.keys())
parser_config_fields = set(config_class.model_fields.keys())
all_options = {
k: v for k, v in generate_config.dict().items() if k in parser_config_fields and k not in additional_options
k: v
for k, v in generate_config.model_dump().items()
if k in parser_config_fields and k not in additional_options
} | dict(additional_options)
return config_class.parse_obj(all_options)
return config_class.model_validate(all_options)


def generate( # noqa: PLR0912, PLR0914, PLR0915
Expand Down Expand Up @@ -518,18 +505,11 @@ def generate( # noqa: PLR0912, PLR0914, PLR0915
raise ValueError(msg)

if config is None:
if is_pydantic_v2():
from datamodel_code_generator.model.pydantic_v2 import UnionMode # noqa: PLC0415
from datamodel_code_generator.types import StrictTypes # noqa: PLC0415
from datamodel_code_generator.model.pydantic_v2 import UnionMode # noqa: PLC0415
from datamodel_code_generator.types import StrictTypes # noqa: PLC0415

GenerateConfig.model_rebuild(_types_namespace={"StrictTypes": StrictTypes, "UnionMode": UnionMode})
config = GenerateConfig.model_validate(options)
else:
from datamodel_code_generator.enums import UnionMode # noqa: PLC0415
from datamodel_code_generator.types import StrictTypes # noqa: PLC0415

GenerateConfig.update_forward_refs(StrictTypes=StrictTypes, UnionMode=UnionMode)
config = GenerateConfig(**options)
GenerateConfig.model_rebuild(_types_namespace={"StrictTypes": StrictTypes, "UnionMode": UnionMode})
config = GenerateConfig.model_validate(options)

# Variables that may be modified during processing
input_filename = config.input_filename
Expand Down Expand Up @@ -979,13 +959,6 @@ def __getattr__(name: str) -> Any:
if name in _LAZY_IMPORTS:
import importlib # noqa: PLC0415

if name == "GenerateConfig" and not is_pydantic_v2(): # pragma: no cover
msg = (
f"'{name}' is only available in Pydantic v2 environments. "
"Use 'from datamodel_code_generator.config import GenerateConfig' instead."
)
raise ImportError(msg)

module = importlib.import_module(_LAZY_IMPORTS[name])
return getattr(module, name)
msg = f"module {__name__!r} has no attribute {name!r}"
Expand Down Expand Up @@ -1034,5 +1007,4 @@ def __getattr__(name: str) -> Any:
"generate_dynamic_models", # noqa: F822
]

if is_pydantic_v2(): # pragma: no cover
__all__ += ["GenerateConfig"]
__all__ += ["GenerateConfig"]
Loading
Loading