Skip to content

Commit 59a585c

Browse files
Print message to use __init__.py when using Import Linter without ruff (#753)
* Print message to use `__init__.py` when using Import Linter without ruff * Update tests to reflect new message behaviour
1 parent 4d9c0e9 commit 59a585c

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ name = "usethis._tool.impl"
230230
type = "layers"
231231
layers = [
232232
"pyproject_toml",
233-
"codespell | deptry | import_linter | pyproject_fmt | requirements_txt | ruff",
233+
"codespell | deptry | import_linter | pyproject_fmt | requirements_txt",
234+
"ruff",
234235
"pytest : coverage_py",
235236
"pre_commit",
236237
]

src/usethis/_tool/impl/import_linter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from usethis._config_file import (
1010
DotImportLinterManager,
1111
)
12-
from usethis._console import box_print, warn_print
12+
from usethis._console import box_print, info_print, warn_print
1313
from usethis._integrations.ci.bitbucket.anchor import (
1414
ScriptItemAnchor as BitbucketScriptItemAnchor,
1515
)
@@ -42,6 +42,7 @@
4242
NoConfigValue,
4343
)
4444
from usethis._tool.impl.pre_commit import PreCommitTool
45+
from usethis._tool.impl.ruff import RuffTool
4546
from usethis._tool.pre_commit import PreCommitConfig
4647
from usethis._tool.rule import RuleConfig
4748

@@ -70,6 +71,12 @@ def is_used(self) -> bool:
7071
return super().is_used()
7172

7273
def print_how_to_use(self) -> None:
74+
if not RuffTool().is_used():
75+
# If Ruff is used, we enable the INP rules instead.
76+
info_print("Ensure '__init__.py' files are used in your packages.")
77+
info_print(
78+
"For more info see <https://docs.python.org/3/tutorial/modules.html#packages>"
79+
)
7380
if PreCommitTool().is_used():
7481
if is_uv_used():
7582
box_print(

tests/usethis/_core/test_core_tool.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,8 @@ def test_dependency(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
850850
"✔ Adding dependency 'import-linter' to the 'dev' group in 'pyproject.toml'.\n"
851851
"☐ Install the dependency 'import-linter'.\n"
852852
"✔ Adding Import Linter config to 'pyproject.toml'.\n"
853+
"ℹ Ensure '__init__.py' files are used in your packages.\n" # noqa: RUF001
854+
"ℹ For more info see <https://docs.python.org/3/tutorial/modules.html#packages>\n" # noqa: RUF001
853855
"☐ Run 'lint-imports' to run Import Linter.\n"
854856
)
855857

@@ -1268,6 +1270,8 @@ def test_stdout_when_cant_find_package(
12681270
"⚠ Could not find any importable packages.\n"
12691271
"⚠ Assuming the package name is test-stdout-when-cant-find-pac0.\n"
12701272
"✔ Adding Import Linter config to 'pyproject.toml'.\n"
1273+
"ℹ Ensure '__init__.py' files are used in your packages.\n" # noqa: RUF001
1274+
"ℹ For more info see <https://docs.python.org/3/tutorial/modules.html#packages>\n" # noqa: RUF001
12711275
"☐ Run 'lint-imports' to run Import Linter.\n"
12721276
)
12731277

@@ -1400,6 +1404,8 @@ def test_config(
14001404
"☐ Install the dependency 'import-linter'.\n"
14011405
"✔ Adding Import Linter config to 'pyproject.toml'.\n"
14021406
"✔ Adding hook 'import-linter' to '.pre-commit-config.yaml'.\n"
1407+
"ℹ Ensure '__init__.py' files are used in your packages.\n" # noqa: RUF001
1408+
"ℹ For more info see <https://docs.python.org/3/tutorial/modules.html#packages>\n" # noqa: RUF001
14031409
"☐ Run 'pre-commit run import-linter --all-files' to run Import Linter.\n"
14041410
)
14051411

tests/usethis/_interface/test_tool.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,10 @@ def test_how(self, tmp_path: Path):
187187
assert (
188188
result.output
189189
== """\
190+
ℹ Ensure '__init__.py' files are used in your packages.
191+
ℹ For more info see <https://docs.python.org/3/tutorial/modules.html#packages>
190192
☐ Run 'lint-imports' to run Import Linter.
191-
"""
193+
""" # noqa: RUF001
192194
)
193195

194196

tests/usethis/_tool/impl/test_import_linter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_pre_commit_and_uv(
1515
# Arrange
1616
(tmp_path / "uv.lock").touch()
1717
(tmp_path / ".pre-commit-config.yaml").touch()
18+
(tmp_path / "ruff.toml").touch()
1819

1920
# Act
2021
with change_cwd(tmp_path), files_manager():
@@ -32,6 +33,7 @@ def test_pre_commit_no_uv(
3233
):
3334
# Arrange
3435
(tmp_path / ".pre-commit-config.yaml").touch()
36+
(tmp_path / "ruff.toml").touch()
3537

3638
# Act
3739
with change_cwd(tmp_path), files_manager():
@@ -47,6 +49,7 @@ def test_pre_commit_no_uv(
4749
def test_uv_only(self, tmp_path: Path, capfd: pytest.CaptureFixture[str]):
4850
# Arrange
4951
(tmp_path / "uv.lock").touch()
52+
(tmp_path / "ruff.toml").touch()
5053

5154
# Act
5255
with change_cwd(tmp_path), files_manager():
@@ -58,6 +61,9 @@ def test_uv_only(self, tmp_path: Path, capfd: pytest.CaptureFixture[str]):
5861
assert out == ("☐ Run 'uv run lint-imports' to run Import Linter.\n")
5962

6063
def test_basic(self, tmp_path: Path, capfd: pytest.CaptureFixture[str]):
64+
# Arrange
65+
(tmp_path / "ruff.toml").touch()
66+
6167
# Act
6268
with change_cwd(tmp_path), files_manager():
6369
ImportLinterTool().print_how_to_use()
@@ -66,3 +72,19 @@ def test_basic(self, tmp_path: Path, capfd: pytest.CaptureFixture[str]):
6672
out, err = capfd.readouterr()
6773
assert not err
6874
assert out == ("☐ Run 'lint-imports' to run Import Linter.\n")
75+
76+
def test_ruff_isnt_used(
77+
self, tmp_path: Path, capfd: pytest.CaptureFixture[str]
78+
):
79+
# Act
80+
with change_cwd(tmp_path), files_manager():
81+
ImportLinterTool().print_how_to_use()
82+
83+
# Assert
84+
out, err = capfd.readouterr()
85+
assert not err
86+
assert out == (
87+
"ℹ Ensure '__init__.py' files are used in your packages.\n" # noqa: RUF001
88+
"ℹ For more info see <https://docs.python.org/3/tutorial/modules.html#packages>\n" # noqa: RUF001
89+
"☐ Run 'lint-imports' to run Import Linter.\n"
90+
)

0 commit comments

Comments
 (0)