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

E402 false positive with cell magics in Jupyter notebooks #10357

Closed
jamesmyatt opened this issue Mar 12, 2024 · 1 comment · Fixed by #10545
Closed

E402 false positive with cell magics in Jupyter notebooks #10357

jamesmyatt opened this issue Mar 12, 2024 · 1 comment · Fixed by #10545
Assignees
Labels
notebook Related to (Jupyter) notebooks rule Implementing or modifying a lint rule

Comments

@jamesmyatt
Copy link

jamesmyatt commented Mar 12, 2024

The E402 rule should permit cell magics as well as comments before the import statements. The cell magics must come first.

e.g.

%%time
import expensive_module

I suspect that line magics should also be permitted before import statements, like:

%time import expensive_module
import cheap_module

Error message is:

E402 Module level import not at top of cell
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Mar 12, 2024
@dhruvmanila
Copy link
Member

Yeah, I agree. Certain cell magics like the one you mentioned should come before the actual code.

I don't think we flag E402 if there's a comment before the first import: https://play.ruff.rs/8d6055cc-be7c-41d9-aac3-ef991213116c

@dhruvmanila dhruvmanila added notebook Related to (Jupyter) notebooks and removed needs-decision Awaiting a decision from a maintainer labels Mar 13, 2024
@dhruvmanila dhruvmanila self-assigned this Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
notebook Related to (Jupyter) notebooks rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants