Skip to content

Commit 9e36683

Browse files
Ignore root-level .py files for Import Linter inference
1 parent c92d31e commit 9e36683

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/usethis/_integrations/project/packages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def _get_packages_in_dir(path: Path) -> set[str]:
4949
"""Get the names of packages in the given directory."""
5050
return {
5151
Path(module_name).name
52-
for _, module_name, _ in pkgutil.iter_modules([path.as_posix()])
53-
if not _is_excluded(module_name)
52+
for _, module_name, ispkg in pkgutil.iter_modules([path.as_posix()])
53+
if ispkg and not _is_excluded(module_name)
5454
}
5555

5656

tests/usethis/_integrations/project/test_packages.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,39 @@ def test_multiple_namespace_packages(self, tmp_path: Path):
6262

6363
with change_cwd(tmp_path):
6464
assert get_importable_packages() == {"foo", "bar"}
65+
66+
def test_flat_layout_excludes_root_py_files(self, tmp_path: Path):
67+
"""Test that root-level .py files are excluded in a flat layout.
68+
69+
In a flat layout (no src/ directory), standalone .py files like
70+
setup.py or conftest.py should not be detected as importable packages.
71+
Only directories with __init__.py should be included.
72+
"""
73+
(tmp_path / "foo").mkdir()
74+
(tmp_path / "foo" / "__init__.py").touch()
75+
(tmp_path / "foo" / "bar.py").touch()
76+
# Root-level .py files that should be excluded
77+
(tmp_path / "setup.py").touch()
78+
(tmp_path / "conftest.py").touch()
79+
80+
with change_cwd(tmp_path):
81+
# Should only include "foo" package, not "setup" or "conftest" modules
82+
assert get_importable_packages() == {"foo"}
83+
84+
def test_src_layout_excludes_root_py_files(self, tmp_path: Path):
85+
"""Test that root-level .py files are excluded in a src/ layout.
86+
87+
In a src/ layout, standalone .py files in src/ like __init__.py
88+
(without a package directory) should not be detected as importable packages.
89+
"""
90+
(tmp_path / "src").mkdir()
91+
(tmp_path / "src" / "foo").mkdir()
92+
(tmp_path / "src" / "foo" / "__init__.py").touch()
93+
(tmp_path / "src" / "foo" / "bar.py").touch()
94+
# Root-level .py files in src/ that should be excluded
95+
(tmp_path / "src" / "conftest.py").touch()
96+
(tmp_path / "src" / "helper.py").touch()
97+
98+
with change_cwd(tmp_path):
99+
# Should only include "foo" package, not "conftest" or "helper" modules
100+
assert get_importable_packages() == {"foo"}

0 commit comments

Comments
 (0)