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

Check whether imports are at top of notebook #13025

Open
redeboer opened this issue Aug 21, 2024 · 2 comments
Open

Check whether imports are at top of notebook #13025

redeboer opened this issue Aug 21, 2024 · 2 comments
Labels
needs-decision Awaiting a decision from a maintainer notebook Related to (Jupyter) notebooks rule Implementing or modifying a lint rule

Comments

@redeboer
Copy link

Since #8872, E402 works per cell in Jupyter notebooks. However, in some projects, you may want to have the imports in one cell on the top of the notebook (e.g. to avoid cluttering code cells further below).

Would it be an idea to either introduce:

  1. a new lint.pycodestyle setting like imports-at-top-of-notebook (defaults to False)?
  2. a new E403 rule that is specifically for notebooks that works at notebook level? This is a variant of what @dhruvmanila suggested in Update E402 to work at cell level for notebooks #8872 (comment).

I would suggest 1., to avoid potential rule code clashes (e.g. #2194).

@AlexWaygood AlexWaygood added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer notebook Related to (Jupyter) notebooks labels Aug 21, 2024
@dhruvmanila
Copy link
Member

Thanks for the feature request. I think this is a useful feature.

We also discussed about providing a dedicated code action to move all imports in a Jupyter Notebook to a dedicated code cell at the top. Just want to confirm whether this request is to move the imports to a dedicated code cell at the top or just to the first code cell (might contain other Python code) in the notebook.

@redeboer
Copy link
Author

We also discussed about providing a dedicated code action to move all imports in a Jupyter Notebook to a dedicated code cell at the top.

Awesome!

Just want to confirm whether this request is to move the imports to a dedicated code cell at the top or just to the first code cell (might contain other Python code) in the notebook.

Ah that's a good point. In practice, we usually have a dedicated cell for this yes, but I'm not sure if everyone works like that. Also, we sometimes put global configurations like logging levels or matplotlib styles in that top cell (example here).

Perhaps this could be configurable too? Difficult to think of good keywords here though, maybe imports-at-top-of-notebook = True and imports-at-top-of-notebook = "separate"? Well as I'm writing this, they already sound like a bad idea...

One could even consider implementing a a separate, additional rule that checks whether imports are in a dedicated cell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision Awaiting a decision from a maintainer notebook Related to (Jupyter) notebooks rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants