diff --git a/src/datamodel_code_generator/format.py b/src/datamodel_code_generator/format.py index 97d945683..bad7141fc 100644 --- a/src/datamodel_code_generator/format.py +++ b/src/datamodel_code_generator/format.py @@ -338,7 +338,7 @@ def apply_black(self, code: str) -> str: def apply_ruff_lint(self, code: str) -> str: """Run ruff check with auto-fix on code.""" result = subprocess.run( - ("ruff", "check", "--fix", "-"), + ("ruff", "check", "--fix", "--unsafe-fixes", "-"), input=code.encode(self.encoding), capture_output=True, check=False, @@ -361,7 +361,7 @@ 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", "-"], + [ruff_path, "check", "--fix", "--unsafe-fixes", "-"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, @@ -407,7 +407,7 @@ 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", str(directory)), + ("ruff", "check", "--fix", "--unsafe-fixes", str(directory)), capture_output=True, check=False, cwd=self.settings_path, diff --git a/tests/test_format.py b/tests/test_format.py index 9b62ddea5..befc5a392 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -175,7 +175,11 @@ def test_format_code_ruff_check_formatter(tmp_path: Path, monkeypatch: pytest.Mo assert formatted_code == "output" mock_run.assert_called_once_with( - ("ruff", "check", "--fix", "-"), input=b"input", capture_output=True, check=False, cwd=str(tmp_path) + ("ruff", "check", "--fix", "--unsafe-fixes", "-"), + input=b"input", + capture_output=True, + check=False, + cwd=str(tmp_path), ) @@ -227,7 +231,7 @@ def test_format_directory_ruff_check(tmp_path: Path, monkeypatch: pytest.MonkeyP formatter.format_directory(output_dir) mock_run.assert_called_once_with( - ("ruff", "check", "--fix", str(output_dir)), + ("ruff", "check", "--fix", "--unsafe-fixes", str(output_dir)), capture_output=True, check=False, cwd=str(tmp_path), @@ -270,7 +274,7 @@ def test_format_directory_both_ruff_formatters(tmp_path: Path, monkeypatch: pyte assert mock_run.call_count == 2 mock_run.assert_any_call( - ("ruff", "check", "--fix", str(output_dir)), + ("ruff", "check", "--fix", "--unsafe-fixes", str(output_dir)), capture_output=True, check=False, cwd=str(tmp_path), @@ -323,7 +327,7 @@ def test_generate_with_ruff_batch_formatting(tmp_path: Path) -> None: assert mock_run.call_count == 2 mock_run.assert_any_call( - ("ruff", "check", "--fix", str(output_dir)), + ("ruff", "check", "--fix", "--unsafe-fixes", str(output_dir)), capture_output=True, check=False, cwd=mock.ANY,