Skip to content

Commit 6b680fb

Browse files
Merge pull request #134 from r1chardj0n3s/fix-multipart
Account for the case where the found module does not have an __init__.py
2 parents 5980f56 + b3d6e53 commit 6b680fb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ast
66
import logging
77
import os.path
8+
import sys
89
import textwrap
910
from pathlib import Path
1011

@@ -59,6 +60,11 @@ def test_import_visitor(stmt: str, result: list[str]) -> None:
5960
vis.visit(ast.parse(stmt))
6061
finalise_result = vis.finalise()
6162
assert set(finalise_result.keys()) == set(result)
63+
for value in finalise_result.values():
64+
assert str(value.filename) not in sys.path
65+
assert Path(value.filename).name != "__init__.py"
66+
assert Path(value.filename).is_absolute()
67+
assert Path(value.filename).exists()
6268

6369

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

0 commit comments

Comments
 (0)