Skip to content

Commit b42a72d

Browse files
fix: suppress PLR0913 for use_ruff with new no_apply parameter
Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/b16c53cf-5cad-4be8-9a39-40207f63ba25 Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
1 parent e4bca36 commit b42a72d

8 files changed

Lines changed: 90 additions & 10 deletions

File tree

src/usethis/_core/tool.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def _add_all_tools_pre_commit_configs():
234234
_tool.add_pre_commit_config()
235235

236236

237-
def use_pyproject_fmt(*, remove: bool = False, how: bool = False) -> None:
237+
def use_pyproject_fmt(
238+
*, remove: bool = False, how: bool = False, no_apply: bool = False
239+
) -> None:
238240
"""Add and configure the pyproject-fmt pyproject.toml formatter tool."""
239241
tool = PyprojectFmtTool()
240242

@@ -249,7 +251,8 @@ def use_pyproject_fmt(*, remove: bool = False, how: bool = False) -> None:
249251
tool.add_pre_commit_config()
250252

251253
tool.add_configs()
252-
tool.apply()
254+
if not no_apply:
255+
tool.apply()
253256
tool.print_how_to_use()
254257
else:
255258
tool.remove_configs()
@@ -404,13 +407,14 @@ def _generate_requirements_txt() -> None:
404407
assert_never(backend)
405408

406409

407-
def use_ruff(
410+
def use_ruff( # noqa: PLR0913
408411
*,
409412
remove: bool = False,
410413
how: bool = False,
411414
minimal: bool = False,
412415
linter: bool = True,
413416
formatter: bool = True,
417+
no_apply: bool = False,
414418
) -> None:
415419
"""Add Ruff to the project.
416420
@@ -427,6 +431,7 @@ def use_ruff(
427431
minimal: Don't add any default rules.
428432
linter: Whether to add/remove the Ruff linter.
429433
formatter: Whether to add/remove the Ruff formatter.
434+
no_apply: Don't run the Ruff formatter after adding it.
430435
"""
431436
if how:
432437
tool = RuffTool(
@@ -473,7 +478,8 @@ def use_ruff(
473478
tool.apply_rule_config(rule_config)
474479
tool.add_pre_commit_config()
475480

476-
tool.apply()
481+
if not no_apply:
482+
tool.apply()
477483
tool.print_how_to_use()
478484
else:
479485
tool = RuffTool(

src/usethis/_toolset/format_.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from usethis._tool.impl.base.pyproject_toml import PyprojectTOMLTool
55

66

7-
def use_formatters(remove: bool = False, how: bool = False):
7+
def use_formatters(remove: bool = False, how: bool = False, no_apply: bool = False):
88
"""Add and configure code formatting tools for the project."""
9-
use_ruff(linter=False, formatter=True, remove=remove, how=how)
9+
use_ruff(linter=False, formatter=True, remove=remove, how=how, no_apply=no_apply)
1010
if PyprojectTOMLTool().is_used() and (not remove or how):
11-
use_pyproject_fmt(remove=remove, how=how)
11+
use_pyproject_fmt(remove=remove, how=how, no_apply=no_apply)

src/usethis/_ui/interface/format_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
backend_opt,
99
frozen_opt,
1010
how_opt,
11+
no_apply_opt,
1112
offline_opt,
1213
quiet_opt,
1314
remove_opt,
@@ -21,6 +22,7 @@ def format_(
2122
quiet: bool = quiet_opt,
2223
frozen: bool = frozen_opt,
2324
backend: BackendEnum = backend_opt,
25+
no_apply: bool = no_apply_opt,
2426
) -> None:
2527
"""Add recommended formatters to the project."""
2628
from usethis._config_file import files_manager
@@ -35,7 +37,7 @@ def format_(
3537
files_manager(),
3638
):
3739
try:
38-
use_formatters(remove=remove, how=how)
40+
use_formatters(remove=remove, how=how, no_apply=no_apply)
3941
except UsethisError as err:
4042
err_print(err)
4143
raise typer.Exit(code=1) from None

src/usethis/_ui/interface/tool.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
frozen_opt,
1616
how_opt,
1717
linter_opt,
18+
no_apply_opt,
1819
no_hook_opt,
1920
offline_opt,
2021
quiet_opt,
@@ -233,6 +234,7 @@ def pyproject_fmt(
233234
frozen: bool = frozen_opt,
234235
backend: BackendEnum = backend_opt,
235236
no_hook: bool = no_hook_opt,
237+
no_apply: bool = no_apply_opt,
236238
) -> None:
237239
"""Use the pyproject-fmt linter: opinionated formatting of 'pyproject.toml' files."""
238240
from usethis._config_file import files_manager
@@ -248,7 +250,7 @@ def pyproject_fmt(
248250
),
249251
files_manager(),
250252
):
251-
_run_tool(use_pyproject_fmt, remove=remove, how=how)
253+
_run_tool(use_pyproject_fmt, remove=remove, how=how, no_apply=no_apply)
252254

253255

254256
@app.command(
@@ -358,6 +360,7 @@ def ruff(
358360
linter: bool = linter_opt,
359361
formatter: bool = formatter_opt,
360362
no_hook: bool = no_hook_opt,
363+
no_apply: bool = no_apply_opt,
361364
) -> None:
362365
"""Use Ruff: an extremely fast Python linter and code formatter."""
363366
from usethis._config_file import files_manager
@@ -373,7 +376,14 @@ def ruff(
373376
),
374377
files_manager(),
375378
):
376-
_run_tool(use_ruff, remove=remove, how=how, linter=linter, formatter=formatter)
379+
_run_tool(
380+
use_ruff,
381+
remove=remove,
382+
how=how,
383+
linter=linter,
384+
formatter=formatter,
385+
no_apply=no_apply,
386+
)
377387

378388

379389
@app.command(

src/usethis/_ui/options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"--no-hook",
3737
help="Don't add or modify git hook configuration, e.g. pre-commit",
3838
)
39+
no_apply_opt = typer.Option(
40+
False,
41+
"--no-apply",
42+
help="Don't run formatters after adding them.",
43+
)
3944

4045
# author command options
4146
author_name_opt = typer.Option(..., "--name", help="Author name")

tests/usethis/_core/test_core_tool.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,19 @@ def test_added(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
22932293
"☐ Run 'uv run pyproject-fmt pyproject.toml' to run pyproject-fmt.\n"
22942294
)
22952295

2296+
class TestNoApply:
2297+
@pytest.mark.usefixtures("_vary_network_conn")
2298+
def test_skips_apply(
2299+
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
2300+
):
2301+
# Act
2302+
with change_cwd(uv_init_dir), PyprojectTOMLManager():
2303+
use_pyproject_fmt(no_apply=True)
2304+
2305+
# Assert
2306+
out, _ = capfd.readouterr()
2307+
assert "Running pyproject-fmt" not in out
2308+
22962309
@pytest.mark.usefixtures("_vary_network_conn")
22972310
def test_pre_commit_integration(
22982311
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
@@ -3167,6 +3180,16 @@ def test_stdout(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
31673180
"☐ Run 'uv run ruff format' to run the Ruff formatter.\n"
31683181
)
31693182

3183+
@pytest.mark.usefixtures("_vary_network_conn")
3184+
def test_no_apply(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
3185+
# Act
3186+
with change_cwd(uv_init_dir), files_manager():
3187+
use_ruff(no_apply=True)
3188+
3189+
# Assert
3190+
out, _ = capfd.readouterr()
3191+
assert "Running the Ruff formatter" not in out
3192+
31703193
@pytest.mark.usefixtures("_vary_network_conn")
31713194
def test_pre_commit_first(self, uv_init_repo_dir: Path):
31723195
# Act

tests/usethis/_ui/interface/test_format_.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,15 @@ def test_none_backend_no_pyproject_toml(self, tmp_path: Path):
6868
"✔ Adding Ruff config to 'ruff.toml'.\n"
6969
"☐ Run 'ruff format' to run the Ruff formatter.\n"
7070
)
71+
72+
@pytest.mark.usefixtures("_vary_network_conn")
73+
def test_no_apply(self, uv_init_dir: Path):
74+
# Act
75+
runner = CliRunner()
76+
with change_cwd(uv_init_dir):
77+
result = runner.invoke_safe(app, ["format", "--no-apply"])
78+
79+
# Assert
80+
assert result.exit_code == 0, result.output
81+
assert "Running the Ruff formatter" not in result.output
82+
assert "Running pyproject-fmt" not in result.output

tests/usethis/_ui/interface/test_tool.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,17 @@ def test_how(self, tmp_path: Path):
352352
"""
353353
)
354354

355+
@pytest.mark.usefixtures("_vary_network_conn")
356+
def test_no_apply(self, uv_init_dir: Path):
357+
# Act
358+
runner = CliRunner()
359+
with change_cwd(uv_init_dir):
360+
result = runner.invoke_safe(app, ["pyproject-fmt", "--no-apply"])
361+
362+
# Assert
363+
assert result.exit_code == 0, result.output
364+
assert "Running pyproject-fmt" not in result.output
365+
355366

356367
class TestPreCommit:
357368
@pytest.mark.usefixtures("_vary_network_conn")
@@ -557,6 +568,17 @@ def test_no_hook_skips_pre_commit(self, uv_init_dir: Path):
557568
assert "ruff-check" not in hook_ids
558569
assert "ruff-format" not in hook_ids
559570

571+
@pytest.mark.usefixtures("_vary_network_conn")
572+
def test_no_apply(self, uv_init_dir: Path):
573+
# Act
574+
runner = CliRunner()
575+
with change_cwd(uv_init_dir):
576+
result = runner.invoke_safe(app, ["ruff", "--no-apply"])
577+
578+
# Assert
579+
assert result.exit_code == 0, result.output
580+
assert "Running the Ruff formatter" not in result.output
581+
560582

561583
class TestPytest:
562584
@pytest.mark.usefixtures("_vary_network_conn")

0 commit comments

Comments
 (0)