Skip to content

Commit df01ed7

Browse files
Refactor _tool.py into separate modules (#573)
* Draft refactor - still circularity present * Fix import cycles * Test sync of OTHER_TOOLS list for pyproject.toml
1 parent 9db5443 commit df01ed7

36 files changed

+3057
-2704
lines changed

pyproject.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,30 @@ layers = [
196196
containers = [ "usethis._core" ]
197197
exhaustive = true
198198

199+
[[tool.importlinter.contracts]]
200+
name = "usethis._tool"
201+
type = "layers"
202+
layers = [
203+
"all_",
204+
"impl",
205+
"base",
206+
"config | rule",
207+
]
208+
containers = [ "usethis._tool" ]
209+
exhaustive = true
210+
211+
[[tool.importlinter.contracts]]
212+
name = "usethis._tool.impl"
213+
type = "layers"
214+
layers = [
215+
"pyproject_toml",
216+
"codespell | deptry | import_linter | pyproject_fmt | requirements_txt | ruff",
217+
"pytest : coverage",
218+
"pre_commit",
219+
]
220+
containers = [ "usethis._tool.impl" ]
221+
exhaustive = true
222+
199223
[[tool.importlinter.contracts]]
200224
name = "usethis._integrations"
201225
type = "layers"

src/usethis/_core/ci.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
remove_bitbucket_pipeline_config,
77
)
88
from usethis._integrations.uv.init import ensure_pyproject_toml
9-
from usethis._tool import (
10-
CodespellTool,
11-
DeptryTool,
12-
PreCommitTool,
13-
PyprojectFmtTool,
14-
PytestTool,
15-
RuffTool,
16-
)
9+
from usethis._tool.impl.codespell import CodespellTool
10+
from usethis._tool.impl.deptry import DeptryTool
11+
from usethis._tool.impl.pre_commit import PreCommitTool
12+
from usethis._tool.impl.pyproject_fmt import PyprojectFmtTool
13+
from usethis._tool.impl.pytest import PytestTool
14+
from usethis._tool.impl.ruff import RuffTool
1715

1816

1917
def use_ci_bitbucket(*, remove: bool = False) -> None:

src/usethis/_core/docstyle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Literal
22

33
from usethis._core.tool import use_ruff
4-
from usethis._tool import RuffTool
4+
from usethis._tool.impl.ruff import RuffTool
55
from usethis.errors import UsethisError
66

77

src/usethis/_core/list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
from usethis._core.readme import is_readme_used
99
from usethis._integrations.ci.bitbucket.used import is_bitbucket_used
10-
from usethis._tool import ALL_TOOLS, RuffTool
10+
from usethis._tool.all_ import ALL_TOOLS
11+
from usethis._tool.impl.ruff import RuffTool
1112

1213

1314
class UsageRow(BaseModel):

src/usethis/_core/tool.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,21 @@
1919
from usethis._integrations.pytest.core import add_pytest_dir, remove_pytest_dir
2020
from usethis._integrations.uv.call import call_uv_subprocess
2121
from usethis._integrations.uv.init import ensure_pyproject_toml
22-
from usethis._tool import (
23-
ALL_TOOLS,
24-
CodespellTool,
25-
CoverageTool,
26-
DeptryTool,
27-
ImportLinterTool,
28-
PreCommitTool,
29-
PyprojectFmtTool,
30-
PyprojectTOMLTool,
31-
PytestTool,
32-
RequirementsTxtTool,
33-
RuffTool,
34-
RuleConfig,
35-
)
22+
from usethis._tool.all_ import ALL_TOOLS
23+
from usethis._tool.impl.codespell import CodespellTool
24+
from usethis._tool.impl.coverage import CoverageTool
25+
from usethis._tool.impl.deptry import DeptryTool
26+
from usethis._tool.impl.import_linter import ImportLinterTool
27+
from usethis._tool.impl.pre_commit import PreCommitTool
28+
from usethis._tool.impl.pyproject_fmt import PyprojectFmtTool
29+
from usethis._tool.impl.pyproject_toml import PyprojectTOMLTool
30+
from usethis._tool.impl.pytest import PytestTool
31+
from usethis._tool.impl.requirements_txt import RequirementsTxtTool
32+
from usethis._tool.impl.ruff import RuffTool
33+
from usethis._tool.rule import RuleConfig
3634

3735
if TYPE_CHECKING:
38-
from usethis._tool import Tool
36+
from usethis._tool.base import Tool
3937

4038

4139
def use_codespell(*, remove: bool = False) -> None:

src/usethis/_integrations/ci/bitbucket/steps.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def add_bitbucket_step_in_default(step: Step) -> None:
7676

7777
# Early exit if the step already exists in some sense
7878
for existing_step in existing_steps:
79-
if _steps_are_equivalent(existing_step, step):
79+
if bitbucket_steps_are_equivalent(existing_step, step):
8080
return
8181

8282
# Add the step to the default pipeline
@@ -91,7 +91,7 @@ def add_bitbucket_step_in_default(step: Step) -> None:
9191

9292
# Remove the placeholder step if it already exists
9393
placeholder = _get_placeholder_step()
94-
if not _steps_are_equivalent(placeholder, step):
94+
if not bitbucket_steps_are_equivalent(placeholder, step):
9595
# Only remove the placeholder if it hasn't already been added.
9696
remove_bitbucket_step_from_default(placeholder)
9797

@@ -247,7 +247,7 @@ def _censor_step(
247247

248248
@_censor_step.register(StepItem)
249249
def _(item: StepItem, *, step: Step) -> StepItem | ParallelItem | StageItem | None:
250-
if _steps_are_equivalent(item.step, step):
250+
if bitbucket_steps_are_equivalent(item.step, step):
251251
return None
252252
return item
253253

@@ -265,7 +265,7 @@ def _(item: ParallelItem, *, step: Step) -> StepItem | ParallelItem | StageItem
265265

266266
new_step_items: list[StepItem] = []
267267
for step_item in step_items:
268-
if _steps_are_equivalent(step_item.step, step):
268+
if bitbucket_steps_are_equivalent(step_item.step, step):
269269
continue
270270
new_step_items.append(step_item)
271271

@@ -288,7 +288,7 @@ def _(item: StageItem, *, step: Step) -> StepItem | ParallelItem | StageItem | N
288288

289289
new_step1s = []
290290
for step1 in step1s:
291-
if _steps_are_equivalent(step1tostep(step1), step):
291+
if bitbucket_steps_are_equivalent(step1tostep(step1), step):
292292
continue
293293
new_step1s.append(step1)
294294

@@ -326,7 +326,7 @@ def _add_step_caches_via_doc(
326326
_add_caches_via_doc(cache_by_name, doc=doc)
327327

328328

329-
def _steps_are_equivalent(step1: Step | None, step2: Step) -> bool:
329+
def bitbucket_steps_are_equivalent(step1: Step | None, step2: Step) -> bool:
330330
if step1 is None:
331331
return False
332332

src/usethis/_integrations/pre_commit/hooks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def add_repo(repo: LocalRepo | UriRepo) -> None:
5454
existing_hooks = extract_hook_ids(doc.model)
5555

5656
if not existing_hooks:
57-
if _hook_ids_are_equivalent(hook_config.id, _PLACEHOLDER_ID):
57+
if hook_ids_are_equivalent(hook_config.id, _PLACEHOLDER_ID):
5858
tick_print("Adding placeholder hook to '.pre-commit-config.yaml'.")
5959
else:
6060
tick_print(
@@ -113,12 +113,12 @@ def insert_repo(
113113
# Don't include the placeholder from now on, since we're adding a repo
114114
# which can be there instead.
115115
if not (
116-
len(hooks) == 1 and _hook_ids_are_equivalent(hooks[0].id, _PLACEHOLDER_ID)
116+
len(hooks) == 1 and hook_ids_are_equivalent(hooks[0].id, _PLACEHOLDER_ID)
117117
):
118118
repos.append(repo)
119119

120120
for hook in hooks:
121-
if _hook_ids_are_equivalent(hook.id, predecessor):
121+
if hook_ids_are_equivalent(hook.id, predecessor):
122122
if repo_to_insert.hooks is not None:
123123
for inserted_hook in repo_to_insert.hooks:
124124
tick_print(
@@ -163,7 +163,7 @@ def remove_hook(hook_id: str) -> None:
163163
continue
164164

165165
for hook in repo.hooks:
166-
if _hook_ids_are_equivalent(hook.id, hook_id):
166+
if hook_ids_are_equivalent(hook.id, hook_id):
167167
tick_print(
168168
f"Removing hook '{hook.id}' from '.pre-commit-config.yaml'."
169169
)
@@ -205,7 +205,7 @@ def extract_hook_ids(model: JsonSchemaForPreCommitConfigYaml) -> list[str]:
205205

206206
def _hooks_are_equivalent(hook: HookDefinition, other: HookDefinition) -> bool:
207207
"""Check if two hooks are equivalent."""
208-
if _hook_ids_are_equivalent(hook.id, other.id):
208+
if hook_ids_are_equivalent(hook.id, other.id):
209209
return True
210210

211211
# Same contents, different name
@@ -214,7 +214,7 @@ def _hooks_are_equivalent(hook: HookDefinition, other: HookDefinition) -> bool:
214214
return hook == other
215215

216216

217-
def _hook_ids_are_equivalent(hook_id: str | None, other: str | None) -> bool:
217+
def hook_ids_are_equivalent(hook_id: str | None, other: str | None) -> bool:
218218
"""Check if two hook IDs are equivalent."""
219219
# Same name
220220
if hook_id == other:

src/usethis/_interface/readme.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
)
1515
from usethis._core.readme import add_readme
1616
from usethis._integrations.uv.used import is_uv_used
17-
from usethis._tool import PreCommitTool, RuffTool
17+
from usethis._tool.impl.pre_commit import PreCommitTool
18+
from usethis._tool.impl.ruff import RuffTool
1819
from usethis.errors import UsethisError
1920

2021

0 commit comments

Comments
 (0)