datamodel-code-generator 0.11.12__py3-none-any.whl → 0.45.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.
- datamodel_code_generator/__init__.py +654 -185
- datamodel_code_generator/__main__.py +872 -388
- datamodel_code_generator/arguments.py +798 -0
- datamodel_code_generator/cli_options.py +295 -0
- datamodel_code_generator/format.py +292 -54
- datamodel_code_generator/http.py +85 -10
- datamodel_code_generator/imports.py +152 -43
- datamodel_code_generator/model/__init__.py +138 -1
- datamodel_code_generator/model/base.py +531 -120
- datamodel_code_generator/model/dataclass.py +211 -0
- datamodel_code_generator/model/enum.py +133 -12
- datamodel_code_generator/model/imports.py +22 -0
- datamodel_code_generator/model/msgspec.py +462 -0
- datamodel_code_generator/model/pydantic/__init__.py +30 -25
- datamodel_code_generator/model/pydantic/base_model.py +304 -100
- datamodel_code_generator/model/pydantic/custom_root_type.py +11 -2
- datamodel_code_generator/model/pydantic/dataclass.py +15 -4
- datamodel_code_generator/model/pydantic/imports.py +40 -27
- datamodel_code_generator/model/pydantic/types.py +188 -96
- datamodel_code_generator/model/pydantic_v2/__init__.py +51 -0
- datamodel_code_generator/model/pydantic_v2/base_model.py +268 -0
- datamodel_code_generator/model/pydantic_v2/imports.py +15 -0
- datamodel_code_generator/model/pydantic_v2/root_model.py +35 -0
- datamodel_code_generator/model/pydantic_v2/types.py +143 -0
- datamodel_code_generator/model/scalar.py +124 -0
- datamodel_code_generator/model/template/Enum.jinja2 +15 -2
- datamodel_code_generator/model/template/ScalarTypeAliasAnnotation.jinja2 +6 -0
- datamodel_code_generator/model/template/ScalarTypeAliasType.jinja2 +6 -0
- datamodel_code_generator/model/template/ScalarTypeStatement.jinja2 +6 -0
- datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2 +20 -0
- datamodel_code_generator/model/template/TypeAliasType.jinja2 +20 -0
- datamodel_code_generator/model/template/TypeStatement.jinja2 +20 -0
- datamodel_code_generator/model/template/TypedDict.jinja2 +5 -0
- datamodel_code_generator/model/template/TypedDictClass.jinja2 +25 -0
- datamodel_code_generator/model/template/TypedDictFunction.jinja2 +24 -0
- datamodel_code_generator/model/template/UnionTypeAliasAnnotation.jinja2 +10 -0
- datamodel_code_generator/model/template/UnionTypeAliasType.jinja2 +10 -0
- datamodel_code_generator/model/template/UnionTypeStatement.jinja2 +10 -0
- datamodel_code_generator/model/template/dataclass.jinja2 +50 -0
- datamodel_code_generator/model/template/msgspec.jinja2 +55 -0
- datamodel_code_generator/model/template/pydantic/BaseModel.jinja2 +17 -4
- datamodel_code_generator/model/template/pydantic/BaseModel_root.jinja2 +12 -4
- datamodel_code_generator/model/template/pydantic/Config.jinja2 +1 -1
- datamodel_code_generator/model/template/pydantic/dataclass.jinja2 +15 -2
- datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2 +57 -0
- datamodel_code_generator/model/template/pydantic_v2/ConfigDict.jinja2 +5 -0
- datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2 +48 -0
- datamodel_code_generator/model/type_alias.py +70 -0
- datamodel_code_generator/model/typed_dict.py +161 -0
- datamodel_code_generator/model/types.py +106 -0
- datamodel_code_generator/model/union.py +105 -0
- datamodel_code_generator/parser/__init__.py +30 -12
- datamodel_code_generator/parser/_graph.py +67 -0
- datamodel_code_generator/parser/_scc.py +171 -0
- datamodel_code_generator/parser/base.py +2426 -380
- datamodel_code_generator/parser/graphql.py +652 -0
- datamodel_code_generator/parser/jsonschema.py +2518 -647
- datamodel_code_generator/parser/openapi.py +631 -222
- datamodel_code_generator/py.typed +0 -0
- datamodel_code_generator/pydantic_patch.py +28 -0
- datamodel_code_generator/reference.py +672 -290
- datamodel_code_generator/types.py +521 -145
- datamodel_code_generator/util.py +155 -0
- datamodel_code_generator/watch.py +65 -0
- datamodel_code_generator-0.45.0.dist-info/METADATA +301 -0
- datamodel_code_generator-0.45.0.dist-info/RECORD +69 -0
- {datamodel_code_generator-0.11.12.dist-info → datamodel_code_generator-0.45.0.dist-info}/WHEEL +1 -1
- datamodel_code_generator-0.45.0.dist-info/entry_points.txt +2 -0
- datamodel_code_generator/version.py +0 -1
- datamodel_code_generator-0.11.12.dist-info/METADATA +0 -440
- datamodel_code_generator-0.11.12.dist-info/RECORD +0 -31
- datamodel_code_generator-0.11.12.dist-info/entry_points.txt +0 -3
- {datamodel_code_generator-0.11.12.dist-info → datamodel_code_generator-0.45.0.dist-info/licenses}/LICENSE +0 -0
|
@@ -0,0 +1,798 @@
|
|
|
1
|
+
"""CLI argument definitions for datamodel-codegen.
|
|
2
|
+
|
|
3
|
+
Defines the ArgumentParser and all command-line options organized into groups:
|
|
4
|
+
base options, typing customization, field customization, model customization,
|
|
5
|
+
template customization, OpenAPI-specific options, and general options.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import locale
|
|
12
|
+
from argparse import ArgumentParser, ArgumentTypeError, BooleanOptionalAction, Namespace, RawDescriptionHelpFormatter
|
|
13
|
+
from operator import attrgetter
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import TYPE_CHECKING, cast
|
|
16
|
+
|
|
17
|
+
from datamodel_code_generator import (
|
|
18
|
+
DEFAULT_SHARED_MODULE_NAME,
|
|
19
|
+
AllExportsCollisionStrategy,
|
|
20
|
+
AllExportsScope,
|
|
21
|
+
AllOfMergeMode,
|
|
22
|
+
DataclassArguments,
|
|
23
|
+
DataModelType,
|
|
24
|
+
InputFileType,
|
|
25
|
+
ModuleSplitMode,
|
|
26
|
+
OpenAPIScope,
|
|
27
|
+
ReadOnlyWriteOnlyModelType,
|
|
28
|
+
ReuseScope,
|
|
29
|
+
)
|
|
30
|
+
from datamodel_code_generator.format import DatetimeClassType, Formatter, PythonVersion
|
|
31
|
+
from datamodel_code_generator.model.pydantic_v2 import UnionMode
|
|
32
|
+
from datamodel_code_generator.parser import LiteralType
|
|
33
|
+
from datamodel_code_generator.types import StrictTypes
|
|
34
|
+
|
|
35
|
+
if TYPE_CHECKING:
|
|
36
|
+
from argparse import Action
|
|
37
|
+
from collections.abc import Iterable
|
|
38
|
+
|
|
39
|
+
DEFAULT_ENCODING = locale.getpreferredencoding()
|
|
40
|
+
|
|
41
|
+
namespace = Namespace(no_color=False)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _dataclass_arguments(value: str) -> DataclassArguments:
|
|
45
|
+
"""Parse JSON string and validate it as DataclassArguments."""
|
|
46
|
+
try:
|
|
47
|
+
result = json.loads(value)
|
|
48
|
+
except json.JSONDecodeError as e:
|
|
49
|
+
msg = f"Invalid JSON: {e}"
|
|
50
|
+
raise ArgumentTypeError(msg) from e
|
|
51
|
+
if not isinstance(result, dict):
|
|
52
|
+
msg = f"Expected a JSON dictionary, got {type(result).__name__}"
|
|
53
|
+
raise ArgumentTypeError(msg)
|
|
54
|
+
valid_keys = set(DataclassArguments.__annotations__.keys())
|
|
55
|
+
invalid_keys = set(result.keys()) - valid_keys
|
|
56
|
+
if invalid_keys:
|
|
57
|
+
msg = f"Invalid keys: {invalid_keys}. Valid keys are: {valid_keys}"
|
|
58
|
+
raise ArgumentTypeError(msg)
|
|
59
|
+
for key, val in result.items():
|
|
60
|
+
if not isinstance(val, bool):
|
|
61
|
+
msg = f"Expected bool for '{key}', got {type(val).__name__}"
|
|
62
|
+
raise ArgumentTypeError(msg)
|
|
63
|
+
return cast("DataclassArguments", result)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class SortingHelpFormatter(RawDescriptionHelpFormatter):
|
|
67
|
+
"""Help formatter that sorts arguments, adds color to section headers, and preserves epilog formatting."""
|
|
68
|
+
|
|
69
|
+
def _bold_cyan(self, text: str) -> str: # noqa: PLR6301
|
|
70
|
+
"""Wrap text in ANSI bold cyan escape codes."""
|
|
71
|
+
return f"\x1b[36;1m{text}\x1b[0m"
|
|
72
|
+
|
|
73
|
+
def add_arguments(self, actions: Iterable[Action]) -> None:
|
|
74
|
+
"""Add arguments sorted by option strings."""
|
|
75
|
+
actions = sorted(actions, key=attrgetter("option_strings"))
|
|
76
|
+
super().add_arguments(actions)
|
|
77
|
+
|
|
78
|
+
def start_section(self, heading: str | None) -> None:
|
|
79
|
+
"""Start a section with optional colored heading."""
|
|
80
|
+
return super().start_section(heading if namespace.no_color or not heading else self._bold_cyan(heading))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
arg_parser = ArgumentParser(
|
|
84
|
+
usage="\n datamodel-codegen [options]",
|
|
85
|
+
description="Generate Python data models from schema definitions or structured data\n\n"
|
|
86
|
+
"For detailed usage, see: https://koxudaxi.github.io/datamodel-code-generator",
|
|
87
|
+
epilog="Documentation: https://koxudaxi.github.io/datamodel-code-generator\n"
|
|
88
|
+
"GitHub: https://github.com/koxudaxi/datamodel-code-generator",
|
|
89
|
+
formatter_class=SortingHelpFormatter,
|
|
90
|
+
add_help=False,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
base_options = arg_parser.add_argument_group("Options")
|
|
94
|
+
typing_options = arg_parser.add_argument_group("Typing customization")
|
|
95
|
+
field_options = arg_parser.add_argument_group("Field customization")
|
|
96
|
+
model_options = arg_parser.add_argument_group("Model customization")
|
|
97
|
+
extra_fields_model_options = model_options.add_mutually_exclusive_group()
|
|
98
|
+
template_options = arg_parser.add_argument_group("Template customization")
|
|
99
|
+
openapi_options = arg_parser.add_argument_group("OpenAPI-only options")
|
|
100
|
+
general_options = arg_parser.add_argument_group("General options")
|
|
101
|
+
|
|
102
|
+
# ======================================================================================
|
|
103
|
+
# Base options for input/output
|
|
104
|
+
# ======================================================================================
|
|
105
|
+
base_options.add_argument(
|
|
106
|
+
"--http-headers",
|
|
107
|
+
nargs="+",
|
|
108
|
+
metavar="HTTP_HEADER",
|
|
109
|
+
help='Set headers in HTTP requests to the remote host. (example: "Authorization: Basic dXNlcjpwYXNz")',
|
|
110
|
+
)
|
|
111
|
+
base_options.add_argument(
|
|
112
|
+
"--http-query-parameters",
|
|
113
|
+
nargs="+",
|
|
114
|
+
metavar="HTTP_QUERY_PARAMETERS",
|
|
115
|
+
help='Set query parameters in HTTP requests to the remote host. (example: "ref=branch")',
|
|
116
|
+
)
|
|
117
|
+
base_options.add_argument(
|
|
118
|
+
"--http-ignore-tls",
|
|
119
|
+
help="Disable verification of the remote host's TLS certificate",
|
|
120
|
+
action="store_true",
|
|
121
|
+
default=None,
|
|
122
|
+
)
|
|
123
|
+
base_options.add_argument(
|
|
124
|
+
"--input",
|
|
125
|
+
help="Input file/directory (default: stdin)",
|
|
126
|
+
)
|
|
127
|
+
base_options.add_argument(
|
|
128
|
+
"--input-file-type",
|
|
129
|
+
help="Input file type (default: auto)",
|
|
130
|
+
choices=[i.value for i in InputFileType],
|
|
131
|
+
)
|
|
132
|
+
base_options.add_argument(
|
|
133
|
+
"--output",
|
|
134
|
+
help="Output file (default: stdout)",
|
|
135
|
+
)
|
|
136
|
+
base_options.add_argument(
|
|
137
|
+
"--output-model-type",
|
|
138
|
+
help="Output model type (default: pydantic.BaseModel)",
|
|
139
|
+
choices=[i.value for i in DataModelType],
|
|
140
|
+
)
|
|
141
|
+
base_options.add_argument(
|
|
142
|
+
"--url",
|
|
143
|
+
help="Input file URL. `--input` is ignored when `--url` is used",
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# ======================================================================================
|
|
147
|
+
# Customization options for generated models
|
|
148
|
+
# ======================================================================================
|
|
149
|
+
extra_fields_model_options.add_argument(
|
|
150
|
+
"--allow-extra-fields",
|
|
151
|
+
help="Deprecated: Allow passing extra fields. This flag is deprecated. Use `--extra-fields=allow` instead.",
|
|
152
|
+
action="store_true",
|
|
153
|
+
default=None,
|
|
154
|
+
)
|
|
155
|
+
model_options.add_argument(
|
|
156
|
+
"--allow-population-by-field-name",
|
|
157
|
+
help="Allow population by field name",
|
|
158
|
+
action="store_true",
|
|
159
|
+
default=None,
|
|
160
|
+
)
|
|
161
|
+
model_options.add_argument(
|
|
162
|
+
"--class-name",
|
|
163
|
+
help="Set class name of root model",
|
|
164
|
+
default=None,
|
|
165
|
+
)
|
|
166
|
+
model_options.add_argument(
|
|
167
|
+
"--collapse-root-models",
|
|
168
|
+
action="store_true",
|
|
169
|
+
default=None,
|
|
170
|
+
help="Models generated with a root-type field will be merged into the models using that root-type model",
|
|
171
|
+
)
|
|
172
|
+
model_options.add_argument(
|
|
173
|
+
"--skip-root-model",
|
|
174
|
+
action="store_true",
|
|
175
|
+
default=None,
|
|
176
|
+
help="Skip generating the model for the root schema element",
|
|
177
|
+
)
|
|
178
|
+
model_options.add_argument(
|
|
179
|
+
"--disable-appending-item-suffix",
|
|
180
|
+
help="Disable appending `Item` suffix to model name in an array",
|
|
181
|
+
action="store_true",
|
|
182
|
+
default=None,
|
|
183
|
+
)
|
|
184
|
+
model_options.add_argument(
|
|
185
|
+
"--disable-timestamp",
|
|
186
|
+
help="Disable timestamp on file headers",
|
|
187
|
+
action="store_true",
|
|
188
|
+
default=None,
|
|
189
|
+
)
|
|
190
|
+
model_options.add_argument(
|
|
191
|
+
"--enable-faux-immutability",
|
|
192
|
+
help="Enable faux immutability",
|
|
193
|
+
action="store_true",
|
|
194
|
+
default=None,
|
|
195
|
+
)
|
|
196
|
+
model_options.add_argument(
|
|
197
|
+
"--enable-version-header",
|
|
198
|
+
help="Enable package version on file headers",
|
|
199
|
+
action="store_true",
|
|
200
|
+
default=None,
|
|
201
|
+
)
|
|
202
|
+
model_options.add_argument(
|
|
203
|
+
"--enable-command-header",
|
|
204
|
+
help="Enable command-line options on file headers for reproducibility",
|
|
205
|
+
action="store_true",
|
|
206
|
+
default=None,
|
|
207
|
+
)
|
|
208
|
+
extra_fields_model_options.add_argument(
|
|
209
|
+
"--extra-fields",
|
|
210
|
+
help="Set the generated models to allow, forbid, or ignore extra fields.",
|
|
211
|
+
choices=["allow", "ignore", "forbid"],
|
|
212
|
+
default=None,
|
|
213
|
+
)
|
|
214
|
+
model_options.add_argument(
|
|
215
|
+
"--keep-model-order",
|
|
216
|
+
help="Keep generated models' order",
|
|
217
|
+
action="store_true",
|
|
218
|
+
default=None,
|
|
219
|
+
)
|
|
220
|
+
model_options.add_argument(
|
|
221
|
+
"--keyword-only",
|
|
222
|
+
help="Defined models as keyword only (for example dataclass(kw_only=True)).",
|
|
223
|
+
action="store_true",
|
|
224
|
+
default=None,
|
|
225
|
+
)
|
|
226
|
+
model_options.add_argument(
|
|
227
|
+
"--frozen-dataclasses",
|
|
228
|
+
help="Generate frozen dataclasses (dataclass(frozen=True)). Only applies to dataclass output.",
|
|
229
|
+
action="store_true",
|
|
230
|
+
default=None,
|
|
231
|
+
)
|
|
232
|
+
model_options.add_argument(
|
|
233
|
+
"--dataclass-arguments",
|
|
234
|
+
type=_dataclass_arguments,
|
|
235
|
+
default=None,
|
|
236
|
+
help=(
|
|
237
|
+
"Custom dataclass arguments as a JSON dictionary, "
|
|
238
|
+
'e.g. \'{"frozen": true, "kw_only": true}\'. '
|
|
239
|
+
"Overrides --frozen-dataclasses and similar flags."
|
|
240
|
+
),
|
|
241
|
+
)
|
|
242
|
+
model_options.add_argument(
|
|
243
|
+
"--reuse-model",
|
|
244
|
+
help="Reuse models on the field when a module has the model with the same content",
|
|
245
|
+
action="store_true",
|
|
246
|
+
default=None,
|
|
247
|
+
)
|
|
248
|
+
model_options.add_argument(
|
|
249
|
+
"--reuse-scope",
|
|
250
|
+
help="Scope for model reuse deduplication: module (per-file, default) or tree (cross-file with shared module). "
|
|
251
|
+
"Only effective when --reuse-model is set.",
|
|
252
|
+
choices=[s.value for s in ReuseScope],
|
|
253
|
+
default=None,
|
|
254
|
+
)
|
|
255
|
+
model_options.add_argument(
|
|
256
|
+
"--shared-module-name",
|
|
257
|
+
help=f'Name of the shared module for --reuse-scope=tree (default: "{DEFAULT_SHARED_MODULE_NAME}"). '
|
|
258
|
+
f'Use this option if your schema has a file named "{DEFAULT_SHARED_MODULE_NAME}".',
|
|
259
|
+
default=None,
|
|
260
|
+
)
|
|
261
|
+
model_options.add_argument(
|
|
262
|
+
"--target-python-version",
|
|
263
|
+
help="target python version",
|
|
264
|
+
choices=[v.value for v in PythonVersion],
|
|
265
|
+
)
|
|
266
|
+
model_options.add_argument(
|
|
267
|
+
"--treat-dot-as-module",
|
|
268
|
+
help="treat dotted module names as modules",
|
|
269
|
+
action="store_true",
|
|
270
|
+
default=None,
|
|
271
|
+
)
|
|
272
|
+
model_options.add_argument(
|
|
273
|
+
"--use-schema-description",
|
|
274
|
+
help="Use schema description to populate class docstring",
|
|
275
|
+
action="store_true",
|
|
276
|
+
default=None,
|
|
277
|
+
)
|
|
278
|
+
model_options.add_argument(
|
|
279
|
+
"--use-title-as-name",
|
|
280
|
+
help="use titles as class names of models",
|
|
281
|
+
action="store_true",
|
|
282
|
+
default=None,
|
|
283
|
+
)
|
|
284
|
+
model_options.add_argument(
|
|
285
|
+
"--use-pendulum",
|
|
286
|
+
help="use pendulum instead of datetime",
|
|
287
|
+
action="store_true",
|
|
288
|
+
default=None,
|
|
289
|
+
)
|
|
290
|
+
model_options.add_argument(
|
|
291
|
+
"--use-exact-imports",
|
|
292
|
+
help='import exact types instead of modules, for example: "from .foo import Bar" instead of '
|
|
293
|
+
'"from . import foo" with "foo.Bar"',
|
|
294
|
+
action="store_true",
|
|
295
|
+
default=None,
|
|
296
|
+
)
|
|
297
|
+
model_options.add_argument(
|
|
298
|
+
"--output-datetime-class",
|
|
299
|
+
help="Choose Datetime class between AwareDatetime, NaiveDatetime or datetime. "
|
|
300
|
+
"Each output model has its default mapping (for example pydantic: datetime, dataclass: str, ...)",
|
|
301
|
+
choices=[i.value for i in DatetimeClassType],
|
|
302
|
+
default=None,
|
|
303
|
+
)
|
|
304
|
+
model_options.add_argument(
|
|
305
|
+
"--parent-scoped-naming",
|
|
306
|
+
help="Set name of models defined inline from the parent model",
|
|
307
|
+
action="store_true",
|
|
308
|
+
default=None,
|
|
309
|
+
)
|
|
310
|
+
model_options.add_argument(
|
|
311
|
+
"--all-exports-scope",
|
|
312
|
+
help="Generate __all__ in __init__.py with re-exports. "
|
|
313
|
+
"'children': export from direct child modules only. "
|
|
314
|
+
"'recursive': export from all descendant modules.",
|
|
315
|
+
choices=[s.value for s in AllExportsScope],
|
|
316
|
+
default=None,
|
|
317
|
+
)
|
|
318
|
+
model_options.add_argument(
|
|
319
|
+
"--all-exports-collision-strategy",
|
|
320
|
+
help="Strategy for name collisions when using --all-exports-scope=recursive. "
|
|
321
|
+
"'error': raise an error (default). "
|
|
322
|
+
"'minimal-prefix': add module prefix only to colliding names. "
|
|
323
|
+
"'full-prefix': add full module path prefix to colliding names.",
|
|
324
|
+
choices=[s.value for s in AllExportsCollisionStrategy],
|
|
325
|
+
default=None,
|
|
326
|
+
)
|
|
327
|
+
model_options.add_argument(
|
|
328
|
+
"--module-split-mode",
|
|
329
|
+
help="Split generated models into separate files. 'single': generate one file per model class.",
|
|
330
|
+
choices=[m.value for m in ModuleSplitMode],
|
|
331
|
+
default=None,
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
# ======================================================================================
|
|
335
|
+
# Typing options for generated models
|
|
336
|
+
# ======================================================================================
|
|
337
|
+
typing_options.add_argument(
|
|
338
|
+
"--base-class",
|
|
339
|
+
help="Base Class (default: pydantic.BaseModel)",
|
|
340
|
+
type=str,
|
|
341
|
+
)
|
|
342
|
+
typing_options.add_argument(
|
|
343
|
+
"--enum-field-as-literal",
|
|
344
|
+
help="Parse enum field as literal. "
|
|
345
|
+
"all: all enum field type are Literal. "
|
|
346
|
+
"one: field type is Literal when an enum has only one possible value",
|
|
347
|
+
choices=[lt.value for lt in LiteralType],
|
|
348
|
+
default=None,
|
|
349
|
+
)
|
|
350
|
+
typing_options.add_argument(
|
|
351
|
+
"--field-constraints",
|
|
352
|
+
help="Use field constraints and not con* annotations",
|
|
353
|
+
action="store_true",
|
|
354
|
+
default=None,
|
|
355
|
+
)
|
|
356
|
+
typing_options.add_argument(
|
|
357
|
+
"--set-default-enum-member",
|
|
358
|
+
help="Set enum members as default values for enum field",
|
|
359
|
+
action="store_true",
|
|
360
|
+
default=None,
|
|
361
|
+
)
|
|
362
|
+
typing_options.add_argument(
|
|
363
|
+
"--strict-types",
|
|
364
|
+
help="Use strict types",
|
|
365
|
+
choices=[t.value for t in StrictTypes],
|
|
366
|
+
nargs="+",
|
|
367
|
+
)
|
|
368
|
+
typing_options.add_argument(
|
|
369
|
+
"--use-annotated",
|
|
370
|
+
help="Use typing.Annotated for Field(). Also, `--field-constraints` option will be enabled.",
|
|
371
|
+
action="store_true",
|
|
372
|
+
default=None,
|
|
373
|
+
)
|
|
374
|
+
typing_options.add_argument(
|
|
375
|
+
"--use-serialize-as-any",
|
|
376
|
+
help="Use pydantic.SerializeAsAny for fields with types that have subtypes (Pydantic v2 only)",
|
|
377
|
+
action="store_true",
|
|
378
|
+
default=None,
|
|
379
|
+
)
|
|
380
|
+
typing_options.add_argument(
|
|
381
|
+
"--use-generic-container-types",
|
|
382
|
+
help="Use generic container types for type hinting (typing.Sequence, typing.Mapping). "
|
|
383
|
+
"If `--use-standard-collections` option is set, then import from collections.abc instead of typing",
|
|
384
|
+
action="store_true",
|
|
385
|
+
default=None,
|
|
386
|
+
)
|
|
387
|
+
typing_options.add_argument(
|
|
388
|
+
"--use-non-positive-negative-number-constrained-types",
|
|
389
|
+
help="Use the Non{Positive,Negative}{FloatInt} types instead of the corresponding con* constrained types.",
|
|
390
|
+
action="store_true",
|
|
391
|
+
default=None,
|
|
392
|
+
)
|
|
393
|
+
typing_options.add_argument(
|
|
394
|
+
"--use-decimal-for-multiple-of",
|
|
395
|
+
help="Use condecimal instead of confloat for float/number fields with multipleOf constraint "
|
|
396
|
+
"(Pydantic only). Avoids floating-point precision issues in validation.",
|
|
397
|
+
action="store_true",
|
|
398
|
+
default=None,
|
|
399
|
+
)
|
|
400
|
+
typing_options.add_argument(
|
|
401
|
+
"--use-one-literal-as-default",
|
|
402
|
+
help="Use one literal as default value for one literal field",
|
|
403
|
+
action="store_true",
|
|
404
|
+
default=None,
|
|
405
|
+
)
|
|
406
|
+
typing_options.add_argument(
|
|
407
|
+
"--use-enum-values-in-discriminator",
|
|
408
|
+
help="Use enum member literals in discriminator fields instead of string literals",
|
|
409
|
+
action="store_true",
|
|
410
|
+
default=None,
|
|
411
|
+
)
|
|
412
|
+
typing_options.add_argument(
|
|
413
|
+
"--use-standard-collections",
|
|
414
|
+
help="Use standard collections for type hinting (list, dict)",
|
|
415
|
+
action="store_true",
|
|
416
|
+
default=None,
|
|
417
|
+
)
|
|
418
|
+
typing_options.add_argument(
|
|
419
|
+
"--use-subclass-enum",
|
|
420
|
+
help="Define generic Enum class as subclass with field type when enum has type (int, float, bytes, str)",
|
|
421
|
+
action="store_true",
|
|
422
|
+
default=None,
|
|
423
|
+
)
|
|
424
|
+
typing_options.add_argument(
|
|
425
|
+
"--use-specialized-enum",
|
|
426
|
+
help="Use specialized Enum class (StrEnum, IntEnum). Requires --target-python-version 3.11+",
|
|
427
|
+
action=BooleanOptionalAction,
|
|
428
|
+
default=None,
|
|
429
|
+
)
|
|
430
|
+
typing_options.add_argument(
|
|
431
|
+
"--use-union-operator",
|
|
432
|
+
help="Use | operator for Union type (PEP 604).",
|
|
433
|
+
action="store_true",
|
|
434
|
+
default=None,
|
|
435
|
+
)
|
|
436
|
+
typing_options.add_argument(
|
|
437
|
+
"--use-unique-items-as-set",
|
|
438
|
+
help="define field type as `set` when the field attribute has `uniqueItems`",
|
|
439
|
+
action="store_true",
|
|
440
|
+
default=None,
|
|
441
|
+
)
|
|
442
|
+
typing_options.add_argument(
|
|
443
|
+
"--allof-merge-mode",
|
|
444
|
+
help="Mode for field merging in allOf schemas. "
|
|
445
|
+
"'constraints': merge only constraints (minItems, maxItems, pattern, etc.) from parent (default). "
|
|
446
|
+
"'all': merge constraints plus annotations (default, examples) from parent. "
|
|
447
|
+
"'none': do not merge any fields from parent properties.",
|
|
448
|
+
choices=[m.value for m in AllOfMergeMode],
|
|
449
|
+
default=None,
|
|
450
|
+
)
|
|
451
|
+
typing_options.add_argument(
|
|
452
|
+
"--use-type-alias",
|
|
453
|
+
help="Use TypeAlias instead of root models (experimental)",
|
|
454
|
+
action="store_true",
|
|
455
|
+
default=None,
|
|
456
|
+
)
|
|
457
|
+
typing_options.add_argument(
|
|
458
|
+
"--disable-future-imports",
|
|
459
|
+
help="Disable __future__ imports",
|
|
460
|
+
action="store_true",
|
|
461
|
+
default=None,
|
|
462
|
+
)
|
|
463
|
+
typing_options.add_argument(
|
|
464
|
+
"--type-mappings",
|
|
465
|
+
help="Override default type mappings. "
|
|
466
|
+
'Format: "type+format=target" (e.g., "string+binary=string" to map binary format to string type) '
|
|
467
|
+
'or "format=target" (e.g., "binary=string"). '
|
|
468
|
+
"Can be specified multiple times.",
|
|
469
|
+
nargs="+",
|
|
470
|
+
type=str,
|
|
471
|
+
default=None,
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
# ======================================================================================
|
|
475
|
+
# Customization options for generated model fields
|
|
476
|
+
# ======================================================================================
|
|
477
|
+
field_options.add_argument(
|
|
478
|
+
"--capitalise-enum-members",
|
|
479
|
+
"--capitalize-enum-members",
|
|
480
|
+
help="Capitalize field names on enum",
|
|
481
|
+
action="store_true",
|
|
482
|
+
default=None,
|
|
483
|
+
)
|
|
484
|
+
field_options.add_argument(
|
|
485
|
+
"--empty-enum-field-name",
|
|
486
|
+
help="Set field name when enum value is empty (default: `_`)",
|
|
487
|
+
default=None,
|
|
488
|
+
)
|
|
489
|
+
field_options.add_argument(
|
|
490
|
+
"--field-extra-keys",
|
|
491
|
+
help="Add extra keys to field parameters",
|
|
492
|
+
type=str,
|
|
493
|
+
nargs="+",
|
|
494
|
+
)
|
|
495
|
+
field_options.add_argument(
|
|
496
|
+
"--field-extra-keys-without-x-prefix",
|
|
497
|
+
help="Add extra keys with `x-` prefix to field parameters. The extra keys are stripped of the `x-` prefix.",
|
|
498
|
+
type=str,
|
|
499
|
+
nargs="+",
|
|
500
|
+
)
|
|
501
|
+
field_options.add_argument(
|
|
502
|
+
"--field-include-all-keys",
|
|
503
|
+
help="Add all keys to field parameters",
|
|
504
|
+
action="store_true",
|
|
505
|
+
default=None,
|
|
506
|
+
)
|
|
507
|
+
field_options.add_argument(
|
|
508
|
+
"--force-optional",
|
|
509
|
+
help="Force optional for required fields",
|
|
510
|
+
action="store_true",
|
|
511
|
+
default=None,
|
|
512
|
+
)
|
|
513
|
+
field_options.add_argument(
|
|
514
|
+
"--original-field-name-delimiter",
|
|
515
|
+
help="Set delimiter to convert to snake case. This option only can be used with --snake-case-field (default: `_` )",
|
|
516
|
+
default=None,
|
|
517
|
+
)
|
|
518
|
+
field_options.add_argument(
|
|
519
|
+
"--remove-special-field-name-prefix",
|
|
520
|
+
help="Remove field name prefix if it has a special meaning e.g. underscores",
|
|
521
|
+
action="store_true",
|
|
522
|
+
default=None,
|
|
523
|
+
)
|
|
524
|
+
field_options.add_argument(
|
|
525
|
+
"--snake-case-field",
|
|
526
|
+
help="Change camel-case field name to snake-case",
|
|
527
|
+
action="store_true",
|
|
528
|
+
default=None,
|
|
529
|
+
)
|
|
530
|
+
field_options.add_argument(
|
|
531
|
+
"--special-field-name-prefix",
|
|
532
|
+
help="Set field name prefix when first character can't be used as Python field name (default: `field`)",
|
|
533
|
+
default=None,
|
|
534
|
+
)
|
|
535
|
+
field_options.add_argument(
|
|
536
|
+
"--strip-default-none",
|
|
537
|
+
help="Strip default None on fields",
|
|
538
|
+
action="store_true",
|
|
539
|
+
default=None,
|
|
540
|
+
)
|
|
541
|
+
field_options.add_argument(
|
|
542
|
+
"--use-default",
|
|
543
|
+
help="Use default value even if a field is required",
|
|
544
|
+
action="store_true",
|
|
545
|
+
default=None,
|
|
546
|
+
)
|
|
547
|
+
field_options.add_argument(
|
|
548
|
+
"--use-default-kwarg",
|
|
549
|
+
action="store_true",
|
|
550
|
+
help="Use `default=` instead of a positional argument for Fields that have default values.",
|
|
551
|
+
default=None,
|
|
552
|
+
)
|
|
553
|
+
field_options.add_argument(
|
|
554
|
+
"--use-field-description",
|
|
555
|
+
help="Use schema description to populate field docstring",
|
|
556
|
+
action="store_true",
|
|
557
|
+
default=None,
|
|
558
|
+
)
|
|
559
|
+
field_options.add_argument(
|
|
560
|
+
"--use-attribute-docstrings",
|
|
561
|
+
help="Set use_attribute_docstrings=True in Pydantic v2 ConfigDict",
|
|
562
|
+
action="store_true",
|
|
563
|
+
default=None,
|
|
564
|
+
)
|
|
565
|
+
field_options.add_argument(
|
|
566
|
+
"--use-inline-field-description",
|
|
567
|
+
help="Use schema description to populate field docstring as inline docstring",
|
|
568
|
+
action="store_true",
|
|
569
|
+
default=None,
|
|
570
|
+
)
|
|
571
|
+
field_options.add_argument(
|
|
572
|
+
"--union-mode",
|
|
573
|
+
help="Union mode for only pydantic v2 field",
|
|
574
|
+
choices=[u.value for u in UnionMode],
|
|
575
|
+
default=None,
|
|
576
|
+
)
|
|
577
|
+
field_options.add_argument(
|
|
578
|
+
"--no-alias",
|
|
579
|
+
help="""Do not add a field alias. E.g., if --snake-case-field is used along with a base class, which has an
|
|
580
|
+
alias_generator""",
|
|
581
|
+
action="store_true",
|
|
582
|
+
default=None,
|
|
583
|
+
)
|
|
584
|
+
field_options.add_argument(
|
|
585
|
+
"--use-frozen-field",
|
|
586
|
+
help="Use Field(frozen=True) for readOnly fields (Pydantic v2) or Field(allow_mutation=False) (Pydantic v1)",
|
|
587
|
+
action="store_true",
|
|
588
|
+
default=None,
|
|
589
|
+
)
|
|
590
|
+
|
|
591
|
+
# ======================================================================================
|
|
592
|
+
# Options for templating output
|
|
593
|
+
# ======================================================================================
|
|
594
|
+
template_options.add_argument(
|
|
595
|
+
"--aliases",
|
|
596
|
+
help="Alias mapping file (JSON) for renaming fields. "
|
|
597
|
+
"Supports hierarchical formats: "
|
|
598
|
+
"Flat: {'field': 'alias'} applies to all occurrences. "
|
|
599
|
+
"Scoped: {'ClassName.field': 'alias'} applies to specific class. "
|
|
600
|
+
"Priority: scoped > flat. "
|
|
601
|
+
"Example: {'User.name': 'user_name', 'Address.name': 'addr_name', 'id': 'id_'}",
|
|
602
|
+
type=Path,
|
|
603
|
+
)
|
|
604
|
+
template_options.add_argument(
|
|
605
|
+
"--custom-file-header",
|
|
606
|
+
help="Custom file header",
|
|
607
|
+
type=str,
|
|
608
|
+
default=None,
|
|
609
|
+
)
|
|
610
|
+
template_options.add_argument(
|
|
611
|
+
"--custom-file-header-path",
|
|
612
|
+
help="Custom file header file path",
|
|
613
|
+
default=None,
|
|
614
|
+
type=str,
|
|
615
|
+
)
|
|
616
|
+
template_options.add_argument(
|
|
617
|
+
"--custom-template-dir",
|
|
618
|
+
help="Custom template directory",
|
|
619
|
+
type=str,
|
|
620
|
+
)
|
|
621
|
+
template_options.add_argument(
|
|
622
|
+
"--encoding",
|
|
623
|
+
help=f"The encoding of input and output (default: {DEFAULT_ENCODING})",
|
|
624
|
+
default=None,
|
|
625
|
+
)
|
|
626
|
+
template_options.add_argument(
|
|
627
|
+
"--extra-template-data",
|
|
628
|
+
help="Extra template data for output models. Input is supposed to be a json/yaml file. "
|
|
629
|
+
"For OpenAPI and Jsonschema the keys are the spec path of the object, or the name of the object if you want to "
|
|
630
|
+
"apply the template data to multiple objects with the same name. "
|
|
631
|
+
"If you are using another input file type (e.g. GraphQL), the key is the name of the object. "
|
|
632
|
+
"The value is a dictionary of the template data to add.",
|
|
633
|
+
type=Path,
|
|
634
|
+
)
|
|
635
|
+
template_options.add_argument(
|
|
636
|
+
"--use-double-quotes",
|
|
637
|
+
action="store_true",
|
|
638
|
+
default=None,
|
|
639
|
+
help="Model generated with double quotes. Single quotes or "
|
|
640
|
+
"your black config skip_string_normalization value will be used without this option.",
|
|
641
|
+
)
|
|
642
|
+
template_options.add_argument(
|
|
643
|
+
"--wrap-string-literal",
|
|
644
|
+
help="Wrap string literal by using black `experimental-string-processing` option (require black 20.8b0 or later)",
|
|
645
|
+
action="store_true",
|
|
646
|
+
default=None,
|
|
647
|
+
)
|
|
648
|
+
base_options.add_argument(
|
|
649
|
+
"--additional-imports",
|
|
650
|
+
help='Custom imports for output (delimited list input). For example "datetime.date,datetime.datetime"',
|
|
651
|
+
type=str,
|
|
652
|
+
default=None,
|
|
653
|
+
)
|
|
654
|
+
base_options.add_argument(
|
|
655
|
+
"--formatters",
|
|
656
|
+
help="Formatters for output (default: [black, isort])",
|
|
657
|
+
choices=[f.value for f in Formatter],
|
|
658
|
+
nargs="+",
|
|
659
|
+
default=None,
|
|
660
|
+
)
|
|
661
|
+
base_options.add_argument(
|
|
662
|
+
"--custom-formatters",
|
|
663
|
+
help="List of modules with custom formatter (delimited list input).",
|
|
664
|
+
type=str,
|
|
665
|
+
default=None,
|
|
666
|
+
)
|
|
667
|
+
template_options.add_argument(
|
|
668
|
+
"--custom-formatters-kwargs",
|
|
669
|
+
help="A file with kwargs for custom formatters.",
|
|
670
|
+
type=Path,
|
|
671
|
+
)
|
|
672
|
+
|
|
673
|
+
# ======================================================================================
|
|
674
|
+
# Options specific to OpenAPI input schemas
|
|
675
|
+
# ======================================================================================
|
|
676
|
+
openapi_options.add_argument(
|
|
677
|
+
"--openapi-scopes",
|
|
678
|
+
help="Scopes of OpenAPI model generation (default: schemas)",
|
|
679
|
+
choices=[o.value for o in OpenAPIScope],
|
|
680
|
+
nargs="+",
|
|
681
|
+
default=None,
|
|
682
|
+
)
|
|
683
|
+
openapi_options.add_argument(
|
|
684
|
+
"--strict-nullable",
|
|
685
|
+
help="Treat default field as a non-nullable field (Only OpenAPI)",
|
|
686
|
+
action="store_true",
|
|
687
|
+
default=None,
|
|
688
|
+
)
|
|
689
|
+
openapi_options.add_argument(
|
|
690
|
+
"--use-operation-id-as-name",
|
|
691
|
+
help="use operation id of OpenAPI as class names of models",
|
|
692
|
+
action="store_true",
|
|
693
|
+
default=None,
|
|
694
|
+
)
|
|
695
|
+
openapi_options.add_argument(
|
|
696
|
+
"--include-path-parameters",
|
|
697
|
+
help="Include path parameters in generated parameter models in addition to query parameters (Only OpenAPI)",
|
|
698
|
+
action="store_true",
|
|
699
|
+
default=None,
|
|
700
|
+
)
|
|
701
|
+
openapi_options.add_argument(
|
|
702
|
+
"--validation",
|
|
703
|
+
help="Deprecated: Enable validation (Only OpenAPI). this option is deprecated. it will be removed in future "
|
|
704
|
+
"releases",
|
|
705
|
+
action="store_true",
|
|
706
|
+
default=None,
|
|
707
|
+
)
|
|
708
|
+
openapi_options.add_argument(
|
|
709
|
+
"--read-only-write-only-model-type",
|
|
710
|
+
help="Model generation for readOnly/writeOnly fields: "
|
|
711
|
+
"'request-response' = Request/Response models only (no base model), "
|
|
712
|
+
"'all' = Base + Request + Response models.",
|
|
713
|
+
choices=[e.value for e in ReadOnlyWriteOnlyModelType],
|
|
714
|
+
default=None,
|
|
715
|
+
)
|
|
716
|
+
|
|
717
|
+
# ======================================================================================
|
|
718
|
+
# General options
|
|
719
|
+
# ======================================================================================
|
|
720
|
+
general_options.add_argument(
|
|
721
|
+
"--check",
|
|
722
|
+
action="store_true",
|
|
723
|
+
default=None,
|
|
724
|
+
help="Verify generated files are up-to-date without modifying them. "
|
|
725
|
+
"Exits with code 1 if differences found, 0 if up-to-date. "
|
|
726
|
+
"Useful for CI to ensure generated code is committed.",
|
|
727
|
+
)
|
|
728
|
+
general_options.add_argument(
|
|
729
|
+
"--debug",
|
|
730
|
+
help="show debug message (require \"debug\". `$ pip install 'datamodel-code-generator[debug]'`)",
|
|
731
|
+
action="store_true",
|
|
732
|
+
default=None,
|
|
733
|
+
)
|
|
734
|
+
general_options.add_argument(
|
|
735
|
+
"--disable-warnings",
|
|
736
|
+
help="disable warnings",
|
|
737
|
+
action="store_true",
|
|
738
|
+
default=None,
|
|
739
|
+
)
|
|
740
|
+
general_options.add_argument(
|
|
741
|
+
"-h",
|
|
742
|
+
"--help",
|
|
743
|
+
action="help",
|
|
744
|
+
default="==SUPPRESS==",
|
|
745
|
+
help="show this help message and exit",
|
|
746
|
+
)
|
|
747
|
+
general_options.add_argument(
|
|
748
|
+
"--no-color",
|
|
749
|
+
action="store_true",
|
|
750
|
+
default=False,
|
|
751
|
+
help="disable colorized output",
|
|
752
|
+
)
|
|
753
|
+
general_options.add_argument(
|
|
754
|
+
"--generate-pyproject-config",
|
|
755
|
+
action="store_true",
|
|
756
|
+
default=None,
|
|
757
|
+
help="Generate pyproject.toml configuration from the provided CLI arguments and exit",
|
|
758
|
+
)
|
|
759
|
+
general_options.add_argument(
|
|
760
|
+
"--generate-cli-command",
|
|
761
|
+
action="store_true",
|
|
762
|
+
default=None,
|
|
763
|
+
help="Generate CLI command from pyproject.toml configuration and exit",
|
|
764
|
+
)
|
|
765
|
+
general_options.add_argument(
|
|
766
|
+
"--ignore-pyproject",
|
|
767
|
+
action="store_true",
|
|
768
|
+
default=False,
|
|
769
|
+
help="Ignore pyproject.toml configuration",
|
|
770
|
+
)
|
|
771
|
+
general_options.add_argument(
|
|
772
|
+
"--profile",
|
|
773
|
+
help="Use a named profile from pyproject.toml [tool.datamodel-codegen.profiles.<name>]",
|
|
774
|
+
default=None,
|
|
775
|
+
)
|
|
776
|
+
general_options.add_argument(
|
|
777
|
+
"--watch",
|
|
778
|
+
action="store_true",
|
|
779
|
+
default=None,
|
|
780
|
+
help="Watch input file(s) for changes and regenerate output automatically",
|
|
781
|
+
)
|
|
782
|
+
general_options.add_argument(
|
|
783
|
+
"--watch-delay",
|
|
784
|
+
type=float,
|
|
785
|
+
default=None,
|
|
786
|
+
help="Debounce delay in seconds for watch mode (default: 0.5)",
|
|
787
|
+
)
|
|
788
|
+
general_options.add_argument(
|
|
789
|
+
"--version",
|
|
790
|
+
action="store_true",
|
|
791
|
+
help="show version",
|
|
792
|
+
)
|
|
793
|
+
|
|
794
|
+
__all__ = [
|
|
795
|
+
"DEFAULT_ENCODING",
|
|
796
|
+
"arg_parser",
|
|
797
|
+
"namespace",
|
|
798
|
+
]
|