Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/usethis/_tool/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def is_used(self) -> bool:
"""Whether the tool is being used in the current project.

Three heuristics are used by default:
1. Whether any of the tool's characteristic dev dependencies are in the project.
1. Whether any of the tool's characteristic dependencies are in the project.
2. Whether any of the tool's managed files are in the project.
3. Whether any of the tool's managed config file sections are present.
"""
Expand All @@ -118,19 +118,7 @@ def is_used(self) -> bool:

if not _is_used:
try:
_is_used = any(
is_dep_in_any_group(dep)
for dep in self.get_dev_deps(unconditional=True)
)
except FileDecodeError as err:
decode_err_by_name[err.name] = err

if not _is_used:
try:
_is_used = any(
is_dep_in_any_group(dep)
for dep in self.get_test_deps(unconditional=True)
)
_is_used = self.is_declared_as_dep()
except FileDecodeError as err:
decode_err_by_name[err.name] = err

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

return _is_used

def is_declared_as_dep(self) -> bool:
"""Whether the tool is declared as a dependency in the project.

This is inferred based on whether any of the tools characteristic dependencies
are declared in the project.
"""
# N.B. currently doesn't check core dependencies nor extras.
# Only PEP735 dependency groups.
# See https://github.com/usethis-python/usethis-python/issues/809
_is_declared = False

_is_declared = any(
is_dep_in_any_group(dep) for dep in self.get_dev_deps(unconditional=True)
)

if not _is_declared:
_is_declared = any(
is_dep_in_any_group(dep)
for dep in self.get_test_deps(unconditional=True)
)

return _is_declared

def add_dev_deps(self) -> None:
add_deps_to_group(self.get_dev_deps(), "dev")

Expand Down
19 changes: 19 additions & 0 deletions tests/usethis/_tool/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,25 @@ def preferred_file_manager(self) -> KeyValueFileManager:
"\n⚠ Assuming 'setup.cfg' contains no evidence of my_tool being used.\n"
)

class TestIsDeclaredAsDep:
def test_dev_deps(self, uv_init_dir: Path):
# Arrange
tool = MyTool()

with change_cwd(uv_init_dir), PyprojectTOMLManager():
add_deps_to_group(
[
Dependency(name="black"),
],
"dev",
)

# Act
result = tool.is_declared_as_dep()

# Assert
assert result

class TestAddPreCommitConfig:
def test_no_repo_configs(self, uv_init_dir: Path):
# Arrange
Expand Down