Skip to content

Commit 344aff8

Browse files
committed
Account for the case where the found module does not have an __init__.py
1 parent 5980f56 commit 344aff8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pip_check_reqs/common.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,21 @@ def _add_module(self, modname: str, lineno: int) -> None:
8585

8686
modpath = find_spec_result.origin
8787
assert modpath is not None
88+
modpath_path = Path(modpath)
8889
modname = find_spec_result.name
8990

9091
if modname not in self._modules:
92+
if modpath_path.is_file():
93+
if modpath_path.name == "__init__.py":
94+
modpath_path = modpath_path.parent
95+
else:
96+
# We have this empty "else" so that we are
97+
# not tempted to combine the "is file" and "is __init__"
98+
# checks, and to make sure we have coverage for this case.
99+
pass
91100
self._modules[modname] = FoundModule(
92101
modname=modname,
93-
filename=str(Path(modpath).parent),
102+
filename=str(modpath_path),
94103
)
95104
assert isinstance(self._location, str)
96105
self._modules[modname].locations.append((self._location, lineno))

tests/test_common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def test_import_visitor(stmt: str, result: list[str]) -> None:
5959
vis.visit(ast.parse(stmt))
6060
finalise_result = vis.finalise()
6161
assert set(finalise_result.keys()) == set(result)
62+
for value in finalise_result.values():
63+
assert value.filename != "__init__.py"
64+
assert Path(value.filename).is_absolute()
65+
assert Path(value.filename).exists()
6266

6367

6468
def test_pyfiles_file(tmp_path: Path) -> None:

0 commit comments

Comments
 (0)