Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): use rglob to guess local Python modules #753

Merged
merged 2 commits into from
Jul 5, 2024
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
2 changes: 1 addition & 1 deletion python/deptry/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _is_local_module(path: Path) -> bool:
"""Guess if a module is a local Python module."""
return bool(
(path.is_file() and path.name != "__init__.py" and path.suffix == ".py")
or (path.is_dir() and list(path.glob("*.py")))
or (path.is_dir() and list(path.rglob("*.py")))
)

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions tests/data/project_using_namespace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Empty file.
7 changes: 7 additions & 0 deletions tests/data/project_using_namespace/foo/database/bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from os import chdir, walk
from pathlib import Path

import black
import white as w

from foo import api
17 changes: 17 additions & 0 deletions tests/data/project_using_namespace/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[project]
# PEP 621 project metadata
# See https://www.python.org/dev/peps/pep-0621/
name = "foo"
version = "1.2.3"
requires-python = ">=3.7"
dependencies = ["toml"]

[project.optional-dependencies]
dev = ["black==22.10.0"]

[build-system]
requires = ["setuptools>=61.0.0"]
build-backend = "setuptools.build_meta"

[tool.deptry]
pep621_dev_dependency_groups = ["dev"]
39 changes: 39 additions & 0 deletions tests/functional/cli/test_cli_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

import uuid
from pathlib import Path
from typing import TYPE_CHECKING

import pytest

from tests.functional.utils import Project
from tests.utils import get_issues_report

if TYPE_CHECKING:
from tests.utils import PipVenvFactory


@pytest.mark.xdist_group(name=Project.NAMESPACE)
def test_cli_with_namespace(pip_venv_factory: PipVenvFactory) -> None:
with pip_venv_factory(Project.NAMESPACE) as virtual_env:
issue_report = f"{uuid.uuid4()}.json"
result = virtual_env.run(f"deptry . -o {issue_report}")

assert result.returncode == 1
assert get_issues_report(Path(issue_report)) == [
{
"error": {"code": "DEP004", "message": "'black' imported but declared as a dev dependency"},
"module": "black",
"location": {"file": str(Path("foo/database/bar.py")), "line": 4, "column": 8},
},
{
"error": {"code": "DEP001", "message": "'white' imported but missing from the dependency definitions"},
"module": "white",
"location": {"file": str(Path("foo/database/bar.py")), "line": 5, "column": 8},
},
{
"error": {"code": "DEP002", "message": "'toml' defined as a dependency but not used in the codebase"},
"module": "toml",
"location": {"file": str(Path("pyproject.toml")), "line": None, "column": None},
},
]
1 change: 1 addition & 0 deletions tests/functional/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Project(str, Enum):
FUTURE_DEPRECATED_OBSOLETE_ARGUMENT = "project_with_future_deprecated_obsolete_argument"
GITIGNORE = "project_with_gitignore"
MULTIPLE_SOURCE_DIRECTORIES = "project_with_multiple_source_directories"
NAMESPACE = "project_using_namespace"
PDM = "project_with_pdm"
POETRY = "project_with_poetry"
PYPROJECT_DIFFERENT_DIRECTORY = "project_with_pyproject_different_directory"
Expand Down
Loading