-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
F401 false positive when using Jupyter line magic (%timeit imported_thing()
)
#10454
Comments
@dhruvmanila I assume this is due to today's limitation where ruff doesn't parse out anything coming after the magic command because it could, literally, be anything (a shell command). Do you know if this is documented somewhere, if not, should we add it to the FAQ section? |
That’s not true for all magics, some contain statements. E.g.
So the statement could be parsed out. Apart from So all in all not too daunting. |
Yes, that's correct.
I don't think it's documented anywhere except for in my comment here (#8354 (comment)). We did introduce support for transparent cell magics (#8911), which means we only ignore the line where the magic command is defined, not the subsequent lines which might contain valid Python code.
Thank you for pointing that out. We could potentially support selectively parsing out the code after a line magic but only if it doesn't contain any options. This means that we could support
I quickly scanned through the line magic lists and the ones you've mentioned are the only ones which can have a statement or expression after the magic command. While we could potentially support %time, it would necessitate significant changes in the AST representation to store the parsed-out statement or expression, as well as adjustments to the semantic model and any affected lint rules. However, I don't believe it's worth the effort for a single command, especially one typically used as a one-off command. |
We should update the documentation to make this clear. |
why add this limitation if we could just parse the options? |
We can do that, but it requires writing a parser for the options and keeping that up-to-date with any changes in Jupyter. |
That could end up being easy, depending on two factors:
|
I support doing it, it's just work that someone needs to do :) |
One concern with this approach, and maybe it's not really a big problem, is that how do we differentiate between an option and something which looks similar to an option in the Python statement ( |
By doing what I proposed 😉 there's no ambiguity if we know which options exist and which ones take a parameter and which ones don't. |
I searched for “timeit F401” and didn’t find anything relevant.
Reproducer:
Will show a false positive of F401 (Unused import) for the
import os
line:The text was updated successfully, but these errors were encountered: