Skip to content

Commit

Permalink
Default datetime class and python version (#2196)
Browse files Browse the repository at this point in the history
* fix: get_data_model_types argument target_datetime_class has None default. See #2195

* fix: target_datetime_class defaults to DatetimeClassType.Datetime. See #2195

* fix: set defaults in module to make it easier to move to global defaults

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Koudai Aono <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2024
1 parent 5889d67 commit 7abc1cb
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions datamodel_code_generator/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from __future__ import annotations

import sys
from typing import TYPE_CHECKING, Callable, Iterable, List, NamedTuple, Optional, Type

from .. import DatetimeClassType, PythonVersion
from ..types import DataTypeManager as DataTypeManagerABC
from .base import ConstraintsBase, DataModel, DataModelFieldBase

if TYPE_CHECKING:
from .. import DataModelType, DatetimeClassType, PythonVersion
from .. import DataModelType

DEFAULT_TARGET_DATETIME_CLASS = DatetimeClassType.Datetime
DEFAULT_TARGET_PYTHON_VERSION = PythonVersion(
f'{sys.version_info.major}.{sys.version_info.minor}'
)


class DataModelSet(NamedTuple):
Expand All @@ -20,8 +27,8 @@ class DataModelSet(NamedTuple):

def get_data_model_types(
data_model_type: DataModelType,
target_python_version: PythonVersion,
target_datetime_class: DatetimeClassType,
target_python_version: PythonVersion = DEFAULT_TARGET_PYTHON_VERSION,
target_datetime_class: DatetimeClassType = DEFAULT_TARGET_DATETIME_CLASS,
) -> DataModelSet:
from .. import DataModelType
from . import dataclass, msgspec, pydantic, pydantic_v2, rootmodel, typed_dict
Expand Down Expand Up @@ -53,13 +60,17 @@ def get_data_model_types(
)
elif data_model_type == DataModelType.TypingTypedDict:
return DataModelSet(
data_model=typed_dict.TypedDict
if target_python_version.has_typed_dict
else typed_dict.TypedDictBackport,
data_model=(
typed_dict.TypedDict
if target_python_version.has_typed_dict
else typed_dict.TypedDictBackport
),
root_model=rootmodel.RootModel,
field_model=typed_dict.DataModelField
if target_python_version.has_typed_dict_non_required
else typed_dict.DataModelFieldBackport,
field_model=(
typed_dict.DataModelField
if target_python_version.has_typed_dict_non_required
else typed_dict.DataModelFieldBackport
),
data_type_manager=DataTypeManager,
dump_resolve_reference_action=None,
)
Expand Down

0 comments on commit 7abc1cb

Please sign in to comment.