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

F821 false positive in notebook when using magic command as variables #9648

Closed
mathause opened this issue Jan 26, 2024 · 6 comments · Fixed by #9653
Closed

F821 false positive in notebook when using magic command as variables #9648

mathause opened this issue Jan 26, 2024 · 6 comments · Fixed by #9653
Assignees
Labels
bug Something isn't working

Comments

@mathause
Copy link

ruff gives false positives for F821 (Undefined name) in notebooks when using magic command as variables. Ok, this is very specific and potentially related to #9644.

In a jupyter notebook when defining a variable which has the same name as a ipython magic command and using this variable in a following cell and not as the first command triggers F821 (wrongly).

Only classify magic commands as magic if they are preceded with a %?

command

ruff check --no-cache --select F821 test.ipynb

version

ruff --version
ruff 0.1.14

MVCE

(I am not sure how to create a MVCE for a jupyter notebook, but I guess it's easy enough...)

image

@charliermarsh
Copy link
Member

Yeah we can probably improve our heuristics here to specifically detect assignments. The automagics are actually really complicated because it means that (in general) cells may or may not be unsafe depending on the order of their execution (which we can't know). For example: #8398 (comment).

@charliermarsh
Copy link
Member

I'll try to fix this specific case today :)

@charliermarsh charliermarsh added the bug Something isn't working label Jan 26, 2024
@charliermarsh
Copy link
Member

But I need to audit the list of automagics which I dread 😂

@charliermarsh
Copy link
Member

Fixed in #9653.

charliermarsh added a commit that referenced this issue Jan 29, 2024
## Summary

Given a statement like `colors = 6`, we currently treat the cell as an
automagic (since `colors` is an automagic) -- i.e., we assume it's
equivalent to `%colors = 6`. This PR adds some additional detection
whereby if the statement is an _assignment_, we avoid treating it as
such. I audited the list of automagics, and I believe this is safe for
all of them.

Closes #8526.

Closes #9648.

## Test Plan

`cargo test`
@mathause
Copy link
Author

Thanks!

@charliermarsh
Copy link
Member

Of course! If you see more of these, let us know, since we have to rely on some heuristics for this given the flexibility of notebooks.

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