seddonym / grimp

Builds a graph of a Python project's internal dependencies.
https://grimp.readthedocs.io
BSD 2-Clause "Simplified" License
51 stars 10 forks source link

Raise ModuleNotPresent instead of KeyError from find_modules_that_directly_import #113

Open pydanny opened 1 year ago

pydanny commented 1 year ago

To produce the error:

import grimp
graph = grimp.build_graph("nifty_library", include_external_packages=True)
graph.find_modules_that_directly_import('yaml')

This results in this error:

----> 1 graph.find_modules_that_directly_import('yaml')

File ~/.virtualenvs/kraken-core/lib/python3.10/site-packages/grimp/adaptors/graph.py:234, in ImportGraph.find_modules_that_directly_import(self, module)
    233 def find_modules_that_directly_import(self, module: str) -> Set[str]:
--> 234     return self._importers_by_imported[module]

KeyError: 'yaml'
seddonym commented 1 year ago

Thanks for the bug report, yes I agree we could do better here. I think we should raise grimp.exceptions.ModuleNotPresent in this case, which is already raised from some other methods under similar circumstances.