Dependencies with `importlib.import_module` are not detected
lmmx opened this issue · 3 comments
lmmx commented
Describe the bug
- It is possible to declare dependencies using
importlib.import_module("foo")
rather thanimport foo
- These are not identified by
deptry
To Reproduce
Steps to reproduce the behavior:
- Add
tests/data/some_dyn_imports.py
from importlib import import_module
import_module("polars")
- Add test to
tests/unit/imports/test_extract.py
:
def test_dyn_import_parser_py() -> None:
some_dyn_imports_path = Path("tests/data/some_dyn_imports.py")
assert get_imported_modules_from_list_of_files([some_dyn_imports_path]) == {
"importlib": [Location(some_dyn_imports_path, line=1, column=1)],
"polars": [Location(some_dyn_imports_path, line=3, column=1)],
}
Expected behavior
The test should pass in cases where the import argument is specified.
System:
- OS: e.g. Linux Mint 21
- Language Version: Python 3.12
- PDM version: PDM 2.17.1
Edit I extended the node visitor to accept nodes of this type, initial version captures
import importlib
->importlib.import_module("a")
lmmx@709acb8from importlib import import_module
->import_module("b")
lmmx@025df97from importlib import import_module as im
->im("c")
lmmx@73c790a
mkniewallner commented
This would be a great addition to the library. Will gladly accept a pull request for this feature.
lmmx commented
My pleasure:
Also added a note in the docs usage page to clarify that this is only for string literals (code search shows this is not uncommon in real code)
mkniewallner commented
Resolved by #782.