Skip to content

Commit 5980f56

Browse files
committed
Simplify logic in _ImportVisitor
This is dangerous because I do not fully understand it. Also, in #106, @MrMino has described a number of cases I think we do not test for. My hope is that in the worst case this will lead us to improving our test cases.
1 parent 1be23f9 commit 5980f56

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

pip_check_reqs/common.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,18 @@ def visit_ImportFrom( # noqa: N802, pylint: disable=invalid-name
7575
def _add_module(self, modname: str, lineno: int) -> None:
7676
if self._ignore_modules_function(modname):
7777
return
78-
path = None
79-
progress = []
80-
modpath = None
81-
for modname_part in modname.split("."):
82-
find_spec_result = importlib.util.find_spec(
83-
name=modname_part,
84-
package=path,
85-
)
86-
87-
if find_spec_result is None:
88-
# The component specified at this point is not installed.
89-
break
90-
91-
modpath = find_spec_result.origin
9278

93-
# success! we found *something*
94-
progress.append(modname_part)
95-
96-
if modpath is None:
97-
# the module doesn't actually appear to exist on disk
79+
find_spec_result = importlib.util.find_spec(
80+
name=modname.split(".")[0],
81+
)
82+
if find_spec_result is None:
83+
# The component specified at this point is not installed.
9884
return
9985

100-
modname = ".".join(progress)
86+
modpath = find_spec_result.origin
87+
assert modpath is not None
88+
modname = find_spec_result.name
89+
10190
if modname not in self._modules:
10291
self._modules[modname] = FoundModule(
10392
modname=modname,

tests/test_common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def test_found_module() -> None:
4545
("from pathlib import Path", ["pathlib"]),
4646
("from string import hexdigits", ["string"]),
4747
("import urllib.request", ["urllib"]),
48-
("import spam", []), # don't break because bad programmer
48+
# don't break because bad programmer imported the file we are in
49+
("import spam", []),
4950
("from .foo import bar", []), # don't break on relative imports
5051
("from . import baz", []),
5152
],

0 commit comments

Comments
 (0)