Skip to content

Commit 7e00d0e

Browse files
authored
Add --unsafe-fixes to ruff-check formatter (#2847)
1 parent d0f64f1 commit 7e00d0e

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/datamodel_code_generator/format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def apply_black(self, code: str) -> str:
338338
def apply_ruff_lint(self, code: str) -> str:
339339
"""Run ruff check with auto-fix on code."""
340340
result = subprocess.run(
341-
("ruff", "check", "--fix", "-"),
341+
("ruff", "check", "--fix", "--unsafe-fixes", "-"),
342342
input=code.encode(self.encoding),
343343
capture_output=True,
344344
check=False,
@@ -361,7 +361,7 @@ def apply_ruff_check_and_format(self, code: str) -> str:
361361
"""Run ruff check and format in a single pipeline for better performance."""
362362
ruff_path = self._find_ruff_path()
363363
check_proc = subprocess.Popen( # noqa: S603
364-
[ruff_path, "check", "--fix", "-"],
364+
[ruff_path, "check", "--fix", "--unsafe-fixes", "-"],
365365
stdin=subprocess.PIPE,
366366
stdout=subprocess.PIPE,
367367
stderr=subprocess.PIPE,
@@ -407,7 +407,7 @@ def format_directory(self, directory: Path) -> None:
407407
"""Apply ruff formatting to all Python files in a directory."""
408408
if Formatter.RUFF_CHECK in self.formatters:
409409
subprocess.run( # noqa: S603
410-
("ruff", "check", "--fix", str(directory)),
410+
("ruff", "check", "--fix", "--unsafe-fixes", str(directory)),
411411
capture_output=True,
412412
check=False,
413413
cwd=self.settings_path,

tests/test_format.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ def test_format_code_ruff_check_formatter(tmp_path: Path, monkeypatch: pytest.Mo
175175

176176
assert formatted_code == "output"
177177
mock_run.assert_called_once_with(
178-
("ruff", "check", "--fix", "-"), input=b"input", capture_output=True, check=False, cwd=str(tmp_path)
178+
("ruff", "check", "--fix", "--unsafe-fixes", "-"),
179+
input=b"input",
180+
capture_output=True,
181+
check=False,
182+
cwd=str(tmp_path),
179183
)
180184

181185

@@ -227,7 +231,7 @@ def test_format_directory_ruff_check(tmp_path: Path, monkeypatch: pytest.MonkeyP
227231
formatter.format_directory(output_dir)
228232

229233
mock_run.assert_called_once_with(
230-
("ruff", "check", "--fix", str(output_dir)),
234+
("ruff", "check", "--fix", "--unsafe-fixes", str(output_dir)),
231235
capture_output=True,
232236
check=False,
233237
cwd=str(tmp_path),
@@ -270,7 +274,7 @@ def test_format_directory_both_ruff_formatters(tmp_path: Path, monkeypatch: pyte
270274

271275
assert mock_run.call_count == 2
272276
mock_run.assert_any_call(
273-
("ruff", "check", "--fix", str(output_dir)),
277+
("ruff", "check", "--fix", "--unsafe-fixes", str(output_dir)),
274278
capture_output=True,
275279
check=False,
276280
cwd=str(tmp_path),
@@ -323,7 +327,7 @@ def test_generate_with_ruff_batch_formatting(tmp_path: Path) -> None:
323327

324328
assert mock_run.call_count == 2
325329
mock_run.assert_any_call(
326-
("ruff", "check", "--fix", str(output_dir)),
330+
("ruff", "check", "--fix", "--unsafe-fixes", str(output_dir)),
327331
capture_output=True,
328332
check=False,
329333
cwd=mock.ANY,

0 commit comments

Comments
 (0)