Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependencies with importlib.import_module are not detected #780

Closed
lmmx opened this issue Jul 27, 2024 · 3 comments · Fixed by #782
Closed

Dependencies with importlib.import_module are not detected #780

lmmx opened this issue Jul 27, 2024 · 3 comments · Fixed by #782
Labels
bug Something isn't working
Milestone

Comments

@lmmx
Copy link
Contributor

lmmx commented Jul 27, 2024

Describe the bug

  • It is possible to declare dependencies using importlib.import_module("foo") rather than import 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@709acb8
  • from importlib import import_module -> import_module("b") lmmx@025df97
  • from importlib import import_module as im -> im("c") lmmx@73c790a
@lmmx lmmx added the bug Something isn't working label Jul 27, 2024
@mkniewallner
Copy link
Collaborator

This would be a great addition to the library. Will gladly accept a pull request for this feature.

@lmmx
Copy link
Contributor Author

lmmx commented Jul 27, 2024

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 mkniewallner linked a pull request Jul 30, 2024 that will close this issue
4 tasks
@mkniewallner
Copy link
Collaborator

Resolved by #782.

@mkniewallner mkniewallner added this to the 0.18 milestone Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants