Skip to content

Commit 906d62c

Browse files
Add is_declared_as_dep to functionalize checking whether a tool's characteristic dependencies are declared as dependencies (#810)
1 parent 5623dbf commit 906d62c

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

src/usethis/_tool/base.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def is_used(self) -> bool:
105105
"""Whether the tool is being used in the current project.
106106
107107
Three heuristics are used by default:
108-
1. Whether any of the tool's characteristic dev dependencies are in the project.
108+
1. Whether any of the tool's characteristic dependencies are in the project.
109109
2. Whether any of the tool's managed files are in the project.
110110
3. Whether any of the tool's managed config file sections are present.
111111
"""
@@ -118,19 +118,7 @@ def is_used(self) -> bool:
118118

119119
if not _is_used:
120120
try:
121-
_is_used = any(
122-
is_dep_in_any_group(dep)
123-
for dep in self.get_dev_deps(unconditional=True)
124-
)
125-
except FileDecodeError as err:
126-
decode_err_by_name[err.name] = err
127-
128-
if not _is_used:
129-
try:
130-
_is_used = any(
131-
is_dep_in_any_group(dep)
132-
for dep in self.get_test_deps(unconditional=True)
133-
)
121+
_is_used = self.is_declared_as_dep()
134122
except FileDecodeError as err:
135123
decode_err_by_name[err.name] = err
136124

@@ -148,6 +136,29 @@ def is_used(self) -> bool:
148136

149137
return _is_used
150138

139+
def is_declared_as_dep(self) -> bool:
140+
"""Whether the tool is declared as a dependency in the project.
141+
142+
This is inferred based on whether any of the tools characteristic dependencies
143+
are declared in the project.
144+
"""
145+
# N.B. currently doesn't check core dependencies nor extras.
146+
# Only PEP735 dependency groups.
147+
# See https://github.com/usethis-python/usethis-python/issues/809
148+
_is_declared = False
149+
150+
_is_declared = any(
151+
is_dep_in_any_group(dep) for dep in self.get_dev_deps(unconditional=True)
152+
)
153+
154+
if not _is_declared:
155+
_is_declared = any(
156+
is_dep_in_any_group(dep)
157+
for dep in self.get_test_deps(unconditional=True)
158+
)
159+
160+
return _is_declared
161+
151162
def add_dev_deps(self) -> None:
152163
add_deps_to_group(self.get_dev_deps(), "dev")
153164

tests/usethis/_tool/test_base.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,25 @@ def preferred_file_manager(self) -> KeyValueFileManager:
404404
"\n⚠ Assuming 'setup.cfg' contains no evidence of my_tool being used.\n"
405405
)
406406

407+
class TestIsDeclaredAsDep:
408+
def test_dev_deps(self, uv_init_dir: Path):
409+
# Arrange
410+
tool = MyTool()
411+
412+
with change_cwd(uv_init_dir), PyprojectTOMLManager():
413+
add_deps_to_group(
414+
[
415+
Dependency(name="black"),
416+
],
417+
"dev",
418+
)
419+
420+
# Act
421+
result = tool.is_declared_as_dep()
422+
423+
# Assert
424+
assert result
425+
407426
class TestAddPreCommitConfig:
408427
def test_no_repo_configs(self, uv_init_dir: Path):
409428
# Arrange

0 commit comments

Comments
 (0)