datamodel-code-generator 0.27.2__py3-none-any.whl → 0.28.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of datamodel-code-generator might be problematic. Click here for more details.
- datamodel_code_generator/__init__.py +168 -196
- datamodel_code_generator/__main__.py +146 -189
- datamodel_code_generator/arguments.py +227 -230
- datamodel_code_generator/format.py +77 -129
- datamodel_code_generator/http.py +12 -10
- datamodel_code_generator/imports.py +59 -65
- datamodel_code_generator/model/__init__.py +28 -31
- datamodel_code_generator/model/base.py +100 -144
- datamodel_code_generator/model/dataclass.py +62 -70
- datamodel_code_generator/model/enum.py +34 -30
- datamodel_code_generator/model/imports.py +13 -11
- datamodel_code_generator/model/msgspec.py +116 -138
- datamodel_code_generator/model/pydantic/__init__.py +18 -28
- datamodel_code_generator/model/pydantic/base_model.py +121 -140
- datamodel_code_generator/model/pydantic/custom_root_type.py +2 -2
- datamodel_code_generator/model/pydantic/dataclass.py +6 -4
- datamodel_code_generator/model/pydantic/imports.py +35 -33
- datamodel_code_generator/model/pydantic/types.py +91 -119
- datamodel_code_generator/model/pydantic_v2/__init__.py +21 -18
- datamodel_code_generator/model/pydantic_v2/base_model.py +118 -127
- datamodel_code_generator/model/pydantic_v2/imports.py +5 -3
- datamodel_code_generator/model/pydantic_v2/root_model.py +6 -6
- datamodel_code_generator/model/pydantic_v2/types.py +11 -7
- datamodel_code_generator/model/rootmodel.py +1 -1
- datamodel_code_generator/model/scalar.py +33 -32
- datamodel_code_generator/model/typed_dict.py +41 -51
- datamodel_code_generator/model/types.py +24 -19
- datamodel_code_generator/model/union.py +21 -17
- datamodel_code_generator/parser/__init__.py +16 -12
- datamodel_code_generator/parser/base.py +327 -515
- datamodel_code_generator/parser/graphql.py +87 -119
- datamodel_code_generator/parser/jsonschema.py +438 -607
- datamodel_code_generator/parser/openapi.py +180 -220
- datamodel_code_generator/pydantic_patch.py +8 -9
- datamodel_code_generator/reference.py +199 -297
- datamodel_code_generator/types.py +149 -215
- datamodel_code_generator/util.py +23 -36
- {datamodel_code_generator-0.27.2.dist-info → datamodel_code_generator-0.28.0.dist-info}/METADATA +10 -5
- datamodel_code_generator-0.28.0.dist-info/RECORD +59 -0
- datamodel_code_generator-0.27.2.dist-info/RECORD +0 -59
- {datamodel_code_generator-0.27.2.dist-info → datamodel_code_generator-0.28.0.dist-info}/WHEEL +0 -0
- {datamodel_code_generator-0.27.2.dist-info → datamodel_code_generator-0.28.0.dist-info}/entry_points.txt +0 -0
- {datamodel_code_generator-0.27.2.dist-info → datamodel_code_generator-0.28.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,39 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
import keyword
|
|
2
|
-
from
|
|
3
|
-
from typing import (
|
|
4
|
-
Any,
|
|
5
|
-
ClassVar,
|
|
6
|
-
DefaultDict,
|
|
7
|
-
Dict,
|
|
8
|
-
Iterator,
|
|
9
|
-
List,
|
|
10
|
-
Optional,
|
|
11
|
-
Tuple,
|
|
12
|
-
)
|
|
4
|
+
from typing import TYPE_CHECKING, Any, ClassVar
|
|
13
5
|
|
|
14
|
-
from datamodel_code_generator.imports import Import
|
|
15
6
|
from datamodel_code_generator.model import DataModel, DataModelFieldBase
|
|
16
7
|
from datamodel_code_generator.model.base import UNDEFINED
|
|
17
8
|
from datamodel_code_generator.model.imports import (
|
|
18
9
|
IMPORT_NOT_REQUIRED,
|
|
19
10
|
IMPORT_NOT_REQUIRED_BACKPORT,
|
|
20
11
|
IMPORT_TYPED_DICT,
|
|
21
|
-
IMPORT_TYPED_DICT_BACKPORT,
|
|
22
12
|
)
|
|
23
|
-
from datamodel_code_generator.reference import Reference
|
|
24
13
|
from datamodel_code_generator.types import NOT_REQUIRED_PREFIX
|
|
25
14
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from collections import defaultdict
|
|
17
|
+
from collections.abc import Iterator
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
|
|
20
|
+
from datamodel_code_generator.reference import Reference
|
|
21
|
+
|
|
22
|
+
from datamodel_code_generator.imports import Import # noqa: TC001
|
|
23
|
+
|
|
24
|
+
escape_characters = str.maketrans({
|
|
25
|
+
"\\": r"\\",
|
|
26
|
+
"'": r"\'",
|
|
27
|
+
"\b": r"\b",
|
|
28
|
+
"\f": r"\f",
|
|
29
|
+
"\n": r"\n",
|
|
30
|
+
"\r": r"\r",
|
|
31
|
+
"\t": r"\t",
|
|
32
|
+
})
|
|
37
33
|
|
|
38
34
|
|
|
39
35
|
def _is_valid_field_name(field: DataModelFieldBase) -> bool:
|
|
@@ -44,23 +40,23 @@ def _is_valid_field_name(field: DataModelFieldBase) -> bool:
|
|
|
44
40
|
|
|
45
41
|
|
|
46
42
|
class TypedDict(DataModel):
|
|
47
|
-
TEMPLATE_FILE_PATH: ClassVar[str] =
|
|
48
|
-
BASE_CLASS: ClassVar[str] =
|
|
49
|
-
DEFAULT_IMPORTS: ClassVar[
|
|
43
|
+
TEMPLATE_FILE_PATH: ClassVar[str] = "TypedDict.jinja2"
|
|
44
|
+
BASE_CLASS: ClassVar[str] = "typing.TypedDict"
|
|
45
|
+
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = (IMPORT_TYPED_DICT,)
|
|
50
46
|
|
|
51
|
-
def __init__(
|
|
47
|
+
def __init__( # noqa: PLR0913
|
|
52
48
|
self,
|
|
53
49
|
*,
|
|
54
50
|
reference: Reference,
|
|
55
|
-
fields:
|
|
56
|
-
decorators:
|
|
57
|
-
base_classes:
|
|
58
|
-
custom_base_class:
|
|
59
|
-
custom_template_dir:
|
|
60
|
-
extra_template_data:
|
|
61
|
-
methods:
|
|
62
|
-
path:
|
|
63
|
-
description:
|
|
51
|
+
fields: list[DataModelFieldBase],
|
|
52
|
+
decorators: list[str] | None = None,
|
|
53
|
+
base_classes: list[Reference] | None = None,
|
|
54
|
+
custom_base_class: str | None = None,
|
|
55
|
+
custom_template_dir: Path | None = None,
|
|
56
|
+
extra_template_data: defaultdict[str, dict[str, Any]] | None = None,
|
|
57
|
+
methods: list[str] | None = None,
|
|
58
|
+
path: Path | None = None,
|
|
59
|
+
description: str | None = None,
|
|
64
60
|
default: Any = UNDEFINED,
|
|
65
61
|
nullable: bool = False,
|
|
66
62
|
keyword_only: bool = False,
|
|
@@ -99,8 +95,8 @@ class TypedDict(DataModel):
|
|
|
99
95
|
|
|
100
96
|
yield from self.fields
|
|
101
97
|
|
|
102
|
-
def render(self, *, class_name:
|
|
103
|
-
|
|
98
|
+
def render(self, *, class_name: str | None = None) -> str:
|
|
99
|
+
return self._render(
|
|
104
100
|
class_name=class_name or self.class_name,
|
|
105
101
|
fields=self.fields,
|
|
106
102
|
decorators=self.decorators,
|
|
@@ -111,20 +107,14 @@ class TypedDict(DataModel):
|
|
|
111
107
|
all_fields=self.all_fields,
|
|
112
108
|
**self.extra_template_data,
|
|
113
109
|
)
|
|
114
|
-
return response
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
class TypedDictBackport(TypedDict):
|
|
118
|
-
BASE_CLASS: ClassVar[str] = 'typing_extensions.TypedDict'
|
|
119
|
-
DEFAULT_IMPORTS: ClassVar[Tuple[Import, ...]] = (IMPORT_TYPED_DICT_BACKPORT,)
|
|
120
110
|
|
|
121
111
|
|
|
122
112
|
class DataModelField(DataModelFieldBase):
|
|
123
|
-
DEFAULT_IMPORTS: ClassVar[
|
|
113
|
+
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = (IMPORT_NOT_REQUIRED,)
|
|
124
114
|
|
|
125
115
|
@property
|
|
126
116
|
def key(self) -> str:
|
|
127
|
-
return (self.original_name or self.name or
|
|
117
|
+
return (self.original_name or self.name or "").translate( # pragma: no cover
|
|
128
118
|
escape_characters
|
|
129
119
|
)
|
|
130
120
|
|
|
@@ -132,7 +122,7 @@ class DataModelField(DataModelFieldBase):
|
|
|
132
122
|
def type_hint(self) -> str:
|
|
133
123
|
type_hint = super().type_hint
|
|
134
124
|
if self._not_required:
|
|
135
|
-
return f
|
|
125
|
+
return f"{NOT_REQUIRED_PREFIX}{type_hint}]"
|
|
136
126
|
return type_hint
|
|
137
127
|
|
|
138
128
|
@property
|
|
@@ -144,7 +134,7 @@ class DataModelField(DataModelFieldBase):
|
|
|
144
134
|
return not self._not_required
|
|
145
135
|
|
|
146
136
|
@property
|
|
147
|
-
def imports(self) ->
|
|
137
|
+
def imports(self) -> tuple[Import, ...]:
|
|
148
138
|
return (
|
|
149
139
|
*super().imports,
|
|
150
140
|
*(self.DEFAULT_IMPORTS if self._not_required else ()),
|
|
@@ -152,4 +142,4 @@ class DataModelField(DataModelFieldBase):
|
|
|
152
142
|
|
|
153
143
|
|
|
154
144
|
class DataModelFieldBackport(DataModelField):
|
|
155
|
-
DEFAULT_IMPORTS: ClassVar[
|
|
145
|
+
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = (IMPORT_NOT_REQUIRED_BACKPORT,)
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
from
|
|
1
|
+
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from typing import TYPE_CHECKING, Any
|
|
4
|
+
|
|
5
|
+
from datamodel_code_generator import DatetimeClassType, PythonVersion, PythonVersionMin
|
|
4
6
|
from datamodel_code_generator.imports import (
|
|
5
7
|
IMPORT_ANY,
|
|
6
8
|
IMPORT_DECIMAL,
|
|
@@ -9,11 +11,14 @@ from datamodel_code_generator.imports import (
|
|
|
9
11
|
from datamodel_code_generator.types import DataType, StrictTypes, Types
|
|
10
12
|
from datamodel_code_generator.types import DataTypeManager as _DataTypeManager
|
|
11
13
|
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import Sequence
|
|
16
|
+
|
|
12
17
|
|
|
13
|
-
def type_map_factory(data_type:
|
|
14
|
-
data_type_int = data_type(type=
|
|
15
|
-
data_type_float = data_type(type=
|
|
16
|
-
data_type_str = data_type(type=
|
|
18
|
+
def type_map_factory(data_type: type[DataType]) -> dict[Types, DataType]:
|
|
19
|
+
data_type_int = data_type(type="int")
|
|
20
|
+
data_type_float = data_type(type="float")
|
|
21
|
+
data_type_str = data_type(type="str")
|
|
17
22
|
return {
|
|
18
23
|
# TODO: Should we support a special type such UUID?
|
|
19
24
|
Types.integer: data_type_int,
|
|
@@ -26,7 +31,7 @@ def type_map_factory(data_type: Type[DataType]) -> Dict[Types, DataType]:
|
|
|
26
31
|
Types.time: data_type_str,
|
|
27
32
|
Types.string: data_type_str,
|
|
28
33
|
Types.byte: data_type_str, # base64 encoded string
|
|
29
|
-
Types.binary: data_type(type=
|
|
34
|
+
Types.binary: data_type(type="bytes"),
|
|
30
35
|
Types.date: data_type_str,
|
|
31
36
|
Types.date_time: data_type_str,
|
|
32
37
|
Types.timedelta: data_type.from_import(IMPORT_TIMEDELTA),
|
|
@@ -44,26 +49,26 @@ def type_map_factory(data_type: Type[DataType]) -> Dict[Types, DataType]:
|
|
|
44
49
|
Types.ipv6: data_type_str,
|
|
45
50
|
Types.ipv4_network: data_type_str,
|
|
46
51
|
Types.ipv6_network: data_type_str,
|
|
47
|
-
Types.boolean: data_type(type=
|
|
52
|
+
Types.boolean: data_type(type="bool"),
|
|
48
53
|
Types.object: data_type.from_import(IMPORT_ANY, is_dict=True),
|
|
49
|
-
Types.null: data_type(type=
|
|
54
|
+
Types.null: data_type(type="None"),
|
|
50
55
|
Types.array: data_type.from_import(IMPORT_ANY, is_list=True),
|
|
51
56
|
Types.any: data_type.from_import(IMPORT_ANY),
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
|
|
55
60
|
class DataTypeManager(_DataTypeManager):
|
|
56
|
-
def __init__(
|
|
61
|
+
def __init__( # noqa: PLR0913, PLR0917
|
|
57
62
|
self,
|
|
58
|
-
python_version: PythonVersion =
|
|
59
|
-
use_standard_collections: bool = False,
|
|
60
|
-
use_generic_container_types: bool = False,
|
|
61
|
-
strict_types:
|
|
62
|
-
use_non_positive_negative_number_constrained_types: bool = False,
|
|
63
|
-
use_union_operator: bool = False,
|
|
64
|
-
use_pendulum: bool = False,
|
|
63
|
+
python_version: PythonVersion = PythonVersionMin,
|
|
64
|
+
use_standard_collections: bool = False, # noqa: FBT001, FBT002
|
|
65
|
+
use_generic_container_types: bool = False, # noqa: FBT001, FBT002
|
|
66
|
+
strict_types: Sequence[StrictTypes] | None = None,
|
|
67
|
+
use_non_positive_negative_number_constrained_types: bool = False, # noqa: FBT001, FBT002
|
|
68
|
+
use_union_operator: bool = False, # noqa: FBT001, FBT002
|
|
69
|
+
use_pendulum: bool = False, # noqa: FBT001, FBT002
|
|
65
70
|
target_datetime_class: DatetimeClassType = DatetimeClassType.Datetime,
|
|
66
|
-
):
|
|
71
|
+
) -> None:
|
|
67
72
|
super().__init__(
|
|
68
73
|
python_version,
|
|
69
74
|
use_standard_collections,
|
|
@@ -75,7 +80,7 @@ class DataTypeManager(_DataTypeManager):
|
|
|
75
80
|
target_datetime_class,
|
|
76
81
|
)
|
|
77
82
|
|
|
78
|
-
self.type_map:
|
|
83
|
+
self.type_map: dict[Types, DataType] = type_map_factory(self.data_type)
|
|
79
84
|
|
|
80
85
|
def get_data_type(
|
|
81
86
|
self,
|
|
@@ -1,39 +1,43 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
from typing import Any, ClassVar, DefaultDict, Dict, List, Optional, Tuple
|
|
3
|
+
from typing import TYPE_CHECKING, Any, ClassVar
|
|
5
4
|
|
|
6
5
|
from datamodel_code_generator.imports import IMPORT_TYPE_ALIAS, IMPORT_UNION, Import
|
|
7
6
|
from datamodel_code_generator.model import DataModel, DataModelFieldBase
|
|
8
7
|
from datamodel_code_generator.model.base import UNDEFINED
|
|
9
|
-
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from collections import defaultdict
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from datamodel_code_generator.reference import Reference
|
|
10
14
|
|
|
11
15
|
|
|
12
16
|
class DataTypeUnion(DataModel):
|
|
13
|
-
TEMPLATE_FILE_PATH: ClassVar[str] =
|
|
14
|
-
BASE_CLASS: ClassVar[str] =
|
|
15
|
-
DEFAULT_IMPORTS: ClassVar[
|
|
17
|
+
TEMPLATE_FILE_PATH: ClassVar[str] = "Union.jinja2"
|
|
18
|
+
BASE_CLASS: ClassVar[str] = ""
|
|
19
|
+
DEFAULT_IMPORTS: ClassVar[tuple[Import, ...]] = (
|
|
16
20
|
IMPORT_TYPE_ALIAS,
|
|
17
21
|
IMPORT_UNION,
|
|
18
22
|
)
|
|
19
23
|
|
|
20
|
-
def __init__(
|
|
24
|
+
def __init__( # noqa: PLR0913
|
|
21
25
|
self,
|
|
22
26
|
*,
|
|
23
27
|
reference: Reference,
|
|
24
|
-
fields:
|
|
25
|
-
decorators:
|
|
26
|
-
base_classes:
|
|
27
|
-
custom_base_class:
|
|
28
|
-
custom_template_dir:
|
|
29
|
-
extra_template_data:
|
|
30
|
-
methods:
|
|
31
|
-
path:
|
|
32
|
-
description:
|
|
28
|
+
fields: list[DataModelFieldBase],
|
|
29
|
+
decorators: list[str] | None = None,
|
|
30
|
+
base_classes: list[Reference] | None = None,
|
|
31
|
+
custom_base_class: str | None = None,
|
|
32
|
+
custom_template_dir: Path | None = None,
|
|
33
|
+
extra_template_data: defaultdict[str, dict[str, Any]] | None = None,
|
|
34
|
+
methods: list[str] | None = None,
|
|
35
|
+
path: Path | None = None,
|
|
36
|
+
description: str | None = None,
|
|
33
37
|
default: Any = UNDEFINED,
|
|
34
38
|
nullable: bool = False,
|
|
35
39
|
keyword_only: bool = False,
|
|
36
|
-
):
|
|
40
|
+
) -> None:
|
|
37
41
|
super().__init__(
|
|
38
42
|
reference=reference,
|
|
39
43
|
fields=fields,
|
|
@@ -1,33 +1,37 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from enum import Enum
|
|
4
|
-
from typing import Callable,
|
|
4
|
+
from typing import Callable, TypeVar
|
|
5
5
|
|
|
6
|
-
TK = TypeVar(
|
|
7
|
-
TV = TypeVar(
|
|
6
|
+
TK = TypeVar("TK")
|
|
7
|
+
TV = TypeVar("TV")
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class LiteralType(Enum):
|
|
11
|
-
All =
|
|
12
|
-
One =
|
|
11
|
+
All = "all"
|
|
12
|
+
One = "one"
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
class DefaultPutDict(
|
|
15
|
+
class DefaultPutDict(dict[TK, TV]):
|
|
16
16
|
def get_or_put(
|
|
17
17
|
self,
|
|
18
18
|
key: TK,
|
|
19
|
-
default:
|
|
20
|
-
default_factory:
|
|
19
|
+
default: TV | None = None,
|
|
20
|
+
default_factory: Callable[[TK], TV] | None = None,
|
|
21
21
|
) -> TV:
|
|
22
22
|
if key in self:
|
|
23
23
|
return self[key]
|
|
24
|
-
|
|
24
|
+
if default: # pragma: no cover
|
|
25
25
|
value = self[key] = default
|
|
26
26
|
return value
|
|
27
|
-
|
|
27
|
+
if default_factory:
|
|
28
28
|
value = self[key] = default_factory(key)
|
|
29
29
|
return value
|
|
30
|
-
|
|
30
|
+
msg = "Not found default and default_factory"
|
|
31
|
+
raise ValueError(msg) # pragma: no cover
|
|
31
32
|
|
|
32
33
|
|
|
33
|
-
__all__ = [
|
|
34
|
+
__all__ = [
|
|
35
|
+
"DefaultPutDict",
|
|
36
|
+
"LiteralType",
|
|
37
|
+
]
|