forked from koxudaxi/datamodel-code-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.py
More file actions
464 lines (375 loc) · 16.5 KB
/
format.py
File metadata and controls
464 lines (375 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
"""Code formatting utilities and Python version handling.
Provides CodeFormatter for applying black, isort, and ruff formatting,
along with PythonVersion enum and DatetimeClassType for output configuration.
"""
from __future__ import annotations
import shutil
import subprocess # noqa: S404
import sys
from enum import Enum
from functools import cached_property, lru_cache
from importlib import import_module
from pathlib import Path
from typing import TYPE_CHECKING, Any
from warnings import warn
from datamodel_code_generator.util import load_toml
if TYPE_CHECKING:
from collections.abc import Sequence
@lru_cache(maxsize=1)
def _get_black() -> Any:
import black as _black # noqa: PLC0415
return _black
@lru_cache(maxsize=1)
def _get_black_mode() -> Any: # pragma: no cover
black = _get_black()
try:
import black.mode # noqa: PLC0415
except ImportError:
return None
else:
return black.mode
@lru_cache(maxsize=1)
def _get_isort() -> Any:
import isort as _isort # noqa: PLC0415
return _isort
class DatetimeClassType(Enum):
"""Output datetime class type options."""
Datetime = "datetime"
Awaredatetime = "AwareDatetime"
Naivedatetime = "NaiveDatetime"
Pastdatetime = "PastDatetime"
Futuredatetime = "FutureDatetime"
class DateClassType(Enum):
"""Output date class type options."""
Date = "date"
Pastdate = "PastDate"
Futuredate = "FutureDate"
class PythonVersion(Enum):
"""Supported Python version targets for code generation."""
PY_310 = "3.10"
PY_311 = "3.11"
PY_312 = "3.12"
PY_313 = "3.13"
PY_314 = "3.14"
@cached_property
def _is_py_310_or_later(self) -> bool: # pragma: no cover
return True # 3.10+ always true since minimum is PY_310
@cached_property
def _is_py_311_or_later(self) -> bool: # pragma: no cover
return self.value != self.PY_310.value # ty: ignore
@cached_property
def _is_py_312_or_later(self) -> bool: # pragma: no cover
return self.value not in {self.PY_310.value, self.PY_311.value} # ty: ignore
@cached_property
def _is_py_313_or_later(self) -> bool:
return self.value not in {self.PY_310.value, self.PY_311.value, self.PY_312.value} # ty: ignore
@cached_property
def _is_py_314_or_later(self) -> bool:
return self.value not in { # ty: ignore
self.PY_310.value, # ty: ignore
self.PY_311.value, # ty: ignore
self.PY_312.value, # ty: ignore
self.PY_313.value, # ty: ignore
}
@property
def has_union_operator(self) -> bool: # pragma: no cover
"""Check if Python version supports the union operator (|)."""
return self._is_py_310_or_later
@property
def has_type_alias(self) -> bool: # pragma: no cover
"""Check if Python version supports TypeAlias.
.. deprecated::
This property is unused and will be removed in a future version.
"""
warn(
"has_type_alias is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
return self._is_py_310_or_later
@property
def has_typed_dict_non_required(self) -> bool:
"""Check if Python version supports TypedDict NotRequired."""
return self._is_py_311_or_later
@property
def has_typed_dict_read_only(self) -> bool:
"""Check if Python version supports TypedDict ReadOnly (PEP 705)."""
return self._is_py_313_or_later
@property
def has_typed_dict_closed(self) -> bool:
"""Check if Python version supports TypedDict closed/extra_items (PEP 728).
PEP 728 is targeted for Python 3.15. Until then, typing_extensions is required.
"""
return False
@property
def has_kw_only_dataclass(self) -> bool:
"""Check if Python version supports kw_only in dataclasses."""
return self._is_py_310_or_later
@property
def has_type_statement(self) -> bool:
"""Check if Python version supports type statements."""
return self._is_py_312_or_later
@property
def has_native_deferred_annotations(self) -> bool:
"""Check if Python version has native deferred annotations (Python 3.14+)."""
return self._is_py_314_or_later
@property
def has_strenum(self) -> bool:
"""Check if Python version supports StrEnum."""
return self._is_py_311_or_later
PythonVersionMin = PythonVersion.PY_310
@lru_cache(maxsize=1)
def _get_black_python_version_map() -> dict[PythonVersion, Any]:
black = _get_black()
return {
v: getattr(black.TargetVersion, f"PY{v.name.split('_')[-1]}")
for v in PythonVersion
if hasattr(black.TargetVersion, f"PY{v.name.split('_')[-1]}")
}
def is_supported_in_black(python_version: PythonVersion) -> bool: # pragma: no cover
"""Check if a Python version is supported by the installed black version."""
return python_version in _get_black_python_version_map()
def black_find_project_root(sources: Sequence[Path]) -> Path:
"""Find the project root directory for black configuration."""
from black import find_project_root as _find_project_root # noqa: PLC0415
project_root = _find_project_root(tuple(str(s) for s in sources))
if isinstance(project_root, tuple):
return project_root[0]
return project_root # pragma: no cover
class Formatter(Enum):
"""Available code formatters for generated output."""
BLACK = "black"
ISORT = "isort"
RUFF_CHECK = "ruff-check"
RUFF_FORMAT = "ruff-format"
DEFAULT_FORMATTERS = [Formatter.BLACK, Formatter.ISORT]
class CodeFormatter:
"""Formats generated code using black, isort, ruff, and custom formatters."""
def __init__( # noqa: PLR0912, PLR0913, PLR0915, PLR0917
self,
python_version: PythonVersion,
settings_path: Path | None = None,
wrap_string_literal: bool | None = None, # noqa: FBT001
skip_string_normalization: bool = True, # noqa: FBT001, FBT002
known_third_party: list[str] | None = None,
custom_formatters: list[str] | None = None,
custom_formatters_kwargs: dict[str, Any] | None = None,
encoding: str = "utf-8",
formatters: list[Formatter] | None = None,
defer_formatting: bool = False, # noqa: FBT001, FBT002
) -> None:
"""Initialize code formatter with configuration for black, isort, ruff, and custom formatters."""
if formatters is None:
warn(
"The default formatters (black, isort) will be replaced by ruff in a future version. "
"To prepare for this change, consider using: formatters=[Formatter.RUFF_FORMAT, Formatter.RUFF_CHECK]. "
"Install ruff with: pip install 'datamodel-code-generator[ruff]'. "
"To suppress this warning, specify formatters explicitly.",
FutureWarning,
stacklevel=2,
)
formatters = list(DEFAULT_FORMATTERS)
if not settings_path:
settings_path = Path.cwd()
elif settings_path.is_file():
settings_path = settings_path.parent
elif not settings_path.exists():
for parent in settings_path.parents:
if parent.exists():
settings_path = parent
break
else:
settings_path = Path.cwd() # pragma: no cover
self.settings_path: str = str(settings_path)
self.formatters = formatters
self.defer_formatting = defer_formatting
self.encoding = encoding
use_black = Formatter.BLACK in formatters
use_isort = Formatter.ISORT in formatters
if use_black:
root = black_find_project_root((settings_path,))
path = root / "pyproject.toml"
if path.is_file():
pyproject_toml = load_toml(path)
config = pyproject_toml.get("tool", {}).get("black", {})
else:
config = {}
black = _get_black()
black_mode = _get_black_mode()
black_kwargs: dict[str, Any] = {}
if wrap_string_literal is not None:
experimental_string_processing = wrap_string_literal
elif black.__version__ < "24.1.0": # pragma: no cover
experimental_string_processing = config.get("experimental-string-processing")
else:
experimental_string_processing = config.get("preview", False) and ( # pragma: no cover
config.get("unstable", False) or "string_processing" in config.get("enable-unstable-feature", [])
)
if experimental_string_processing is not None: # pragma: no cover
if black.__version__.startswith("19."):
warn(
f"black doesn't support `experimental-string-processing` option"
f" for wrapping string literal in {black.__version__}",
stacklevel=2,
)
elif black.__version__ < "24.1.0":
black_kwargs["experimental_string_processing"] = experimental_string_processing
elif experimental_string_processing:
black_kwargs["preview"] = True
black_kwargs["unstable"] = config.get("unstable", False)
black_kwargs["enabled_features"] = {black_mode.Preview.string_processing}
self.black_mode = black.FileMode(
target_versions={_get_black_python_version_map()[python_version]},
line_length=config.get("line-length", black.DEFAULT_LINE_LENGTH),
string_normalization=not skip_string_normalization or not config.get("skip-string-normalization", True),
**black_kwargs,
)
else:
self.black_mode = None # type: ignore[assignment]
if use_isort:
isort = _get_isort()
self.isort_config_kwargs: dict[str, Any] = {}
if known_third_party:
self.isort_config_kwargs["known_third_party"] = known_third_party
if isort.__version__.startswith("4."): # pragma: no cover
self.isort_config = None
else:
self.isort_config = isort.Config(settings_path=self.settings_path, **self.isort_config_kwargs)
else:
self.isort_config_kwargs = {}
self.isort_config = None
self.custom_formatters_kwargs = custom_formatters_kwargs or {}
self.custom_formatters = self._check_custom_formatters(custom_formatters)
def _load_custom_formatter(self, custom_formatter_import: str) -> CustomCodeFormatter:
"""Load and instantiate a custom formatter from a module path."""
import_ = import_module(custom_formatter_import)
if not hasattr(import_, "CodeFormatter"):
msg = f"Custom formatter module `{import_.__name__}` must contains object with name CodeFormatter"
raise NameError(msg)
formatter_class = import_.__getattribute__("CodeFormatter") # noqa: PLC2801
if not issubclass(formatter_class, CustomCodeFormatter):
msg = f"The custom module {custom_formatter_import} must inherit from `datamodel-code-generator`"
raise TypeError(msg)
return formatter_class(formatter_kwargs=self.custom_formatters_kwargs)
def _check_custom_formatters(self, custom_formatters: list[str] | None) -> list[CustomCodeFormatter]:
"""Validate and load all custom formatters."""
if custom_formatters is None:
return []
return [self._load_custom_formatter(custom_formatter_import) for custom_formatter_import in custom_formatters]
def format_code(
self,
code: str,
) -> str:
"""Apply all configured formatters to the code string."""
if Formatter.ISORT in self.formatters:
code = self.apply_isort(code)
if Formatter.BLACK in self.formatters:
code = self.apply_black(code)
if not self.defer_formatting:
has_ruff_check = Formatter.RUFF_CHECK in self.formatters
has_ruff_format = Formatter.RUFF_FORMAT in self.formatters
if has_ruff_check and has_ruff_format:
code = self.apply_ruff_check_and_format(code)
elif has_ruff_check:
code = self.apply_ruff_lint(code)
elif has_ruff_format:
code = self.apply_ruff_formatter(code)
for formatter in self.custom_formatters:
code = formatter.apply(code)
return code
def apply_black(self, code: str) -> str:
"""Format code using black."""
black = _get_black()
return black.format_str(
code,
mode=self.black_mode,
)
def apply_ruff_lint(self, code: str) -> str:
"""Run ruff check with auto-fix on code."""
result = subprocess.run(
("ruff", "check", "--fix", "--unsafe-fixes", "-"),
input=code.encode(self.encoding),
capture_output=True,
check=False,
cwd=self.settings_path,
)
return result.stdout.decode(self.encoding)
def apply_ruff_formatter(self, code: str) -> str:
"""Format code using ruff format."""
result = subprocess.run(
("ruff", "format", "-"),
input=code.encode(self.encoding),
capture_output=True,
check=False,
cwd=self.settings_path,
)
return result.stdout.decode(self.encoding)
def apply_ruff_check_and_format(self, code: str) -> str:
"""Run ruff check and format in a single pipeline for better performance."""
ruff_path = self._find_ruff_path()
check_proc = subprocess.Popen( # noqa: S603
[ruff_path, "check", "--fix", "--unsafe-fixes", "-"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.settings_path,
)
format_proc = subprocess.Popen( # noqa: S603
[ruff_path, "format", "-"],
stdin=check_proc.stdout,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=self.settings_path,
)
if check_proc.stdout: # pragma: no branch
check_proc.stdout.close()
check_proc.communicate(code.encode(self.encoding))
check_proc.stdin.close() # type: ignore[union-attr]
stdout, _ = format_proc.communicate()
check_proc.wait()
return stdout.decode(self.encoding)
@staticmethod
def _find_ruff_path() -> str:
"""Find ruff executable path, checking virtual environment first."""
bin_dir = Path(sys.executable).parent
ruff_name = "ruff.exe" if sys.platform == "win32" else "ruff"
ruff_in_venv = bin_dir / ruff_name
if ruff_in_venv.exists():
return str(ruff_in_venv)
return shutil.which("ruff") or "ruff" # pragma: no cover
def apply_isort(self, code: str) -> str:
"""Sort imports using isort."""
isort = _get_isort()
if self.isort_config is None: # pragma: no cover
return isort.SortImports(
file_contents=code,
settings_path=self.settings_path,
**self.isort_config_kwargs,
).output
return isort.code(code, config=self.isort_config)
def format_directory(self, directory: Path) -> None:
"""Apply ruff formatting to all Python files in a directory."""
if Formatter.RUFF_CHECK in self.formatters:
subprocess.run( # noqa: S603
("ruff", "check", "--fix", "--unsafe-fixes", str(directory)),
capture_output=True,
check=False,
cwd=self.settings_path,
)
if Formatter.RUFF_FORMAT in self.formatters:
subprocess.run( # noqa: S603
("ruff", "format", str(directory)),
capture_output=True,
check=False,
cwd=self.settings_path,
)
class CustomCodeFormatter:
"""Base class for custom code formatters.
Subclasses must implement the apply() method to transform code.
"""
def __init__(self, formatter_kwargs: dict[str, Any]) -> None:
"""Initialize custom formatter with optional keyword arguments."""
self.formatter_kwargs = formatter_kwargs
def apply(self, code: str) -> str:
"""Apply formatting to code. Must be implemented by subclasses."""
raise NotImplementedError