-
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
F841
(unused-variable) false positives in Jupyter notebooks
#8094
Comments
Thanks for opening this issue. Yes, this is the current limitation of linting Notebooks because Ruff considers everything past the IPython escape token ( |
Closing in favor of #8354 |
Perhaps this issue should be reopened since #8354 has now been closed, but this bug still exists in Ruff. |
@tdulcet Can you help me understand the mentioned code? Is it that there can be any valid Python code between the curly braces? !echo "{<any valid Python code can go here>}" And, does that need to be wrapped in quotes? var = "/path/to/foo"
# Does this need to be wrapped in quotes?
!ls {var} |
Ok, I just tried this and it seems that it does need to be quoted which makes me wonder if it's treated as format expression with the global scope passed in: "!ls {var}".format(globals()) I'll look at the source code later to check this. |
Yes, any valid Python code can be between the braces, except code that contains more braces, so no f-strings or format strings.
This specific example does not need to be wrapped in quotes, but in general it does if the variable could contain whitespace or other special characters, to prevent globbing and word splitting by the shell: var = "/path/to/foo bar" # Variable with whitespace
# This needs to be wrapped in quotes
!ls "{var}" Note that this is obviously not a robust solution against potenchally malicious inputs, where the string contains quote characters itself: var = '/path/to/foo"; rm -rf /"'
# Oh no!
!ls "{var}" In this case, one would likely need to use shlex.quote: import shlex
var = "/path/to/foo; rm -rf /"
# No quotes needed
!ls {shlex.quote(var)} |
The same issue can can
|
or
(Note that one can put arbitrary Python expressions/code in the above brackets, which is not currently linted.)
Ruff actual output:
Ruff expected output: None
ruff /path/to/file.py --fix
), ideally including the--isolated
flag.pyproject.toml
).None
ruff --version
).There is a public notebook here that currently triggers three of these false positives, if you want some real world examples.
Possibly related to #5188 and #1079.
The text was updated successfully, but these errors were encountered: