fredrikaverpil / creosote

Identify unused dependencies and avoid a bloated virtual environment.
MIT License
326 stars 8 forks source link

Add support for notebooks (.ipynb files) when searching for used imports #216

Closed pmason314 closed 1 month ago

pmason314 commented 1 month ago

What feature request would you like to suggest?

Hi, I'm a fan of creosote so thank you for all of your work on it! I'd like to suggest support for Jupyter notebooks; specifically that creosote should look at the contents of .ipynb files when parsing for used imports. This is one of my biggest pain points when using creosote, usually in one of the following ways:

In both cases, it often happens that these notebooks have the only usages of some packages (data visualization packages like matplotlib and seaborn are common culprits). It's an okay user experience to configure those to be ignored in pyproject.toml, but especially in the small project use case I find myself either marking many packages as ignored or disabling the creosote precommit check entirely.

All that said, I know AST-parsing notebooks doesn't exactly work well. However, I do have a local branch with working code that uses nbconvert and tempfile to convert notebooks to .py files, which creosote then parses the imports for correctly. As far as I've been able to test, this also handles notebook "magics" like %autoreload that wouldn't be valid Python syntax.

Will the feature request break backwards compatibility?

No, this would extend the current checks from just .py files.

github-actions[bot] commented 1 month ago

This issue has been open for 7 days with no activity. Remove the stale label or add a comment or it will be closed in 3 days.

fredrikaverpil commented 1 month ago

Hi @pmason314 and thanks for your suggestion!

I'm not actively in need of this myself and thus won't be implementing anything like this (as I'm focussing efforts elsewhere). But I'm open to PRs, especially if you have a working branch.

pmason314 commented 1 month ago

Thanks @fredrikaverpil! Just submitted the PR, along with a couple questions and configurations that could go one way or another depending on personal preference.