-
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
Support parsing IPython escape commands #8354
Comments
Currently, we don't yet parse the contents of ipython, we merely ignore them. |
@dhruvmanila - What would it take for us to selectively support common magics, i.e., parse the bodies? |
Related #8094. I'll close that one in favor of this and bring any relevant information here. I'm a bit hesitant to support parsing magics as there's no clear grammar for it. There are other reasons as well which I'll outline here. Magic argumentsEach magic command can have any number of CLI type of arguments which needs to be skipped as they aren't valid Python syntax. For example, the
But then how do we differentiate between a CLI type of argument and an actual Python code. That would probably require having an understanding of the supported magic commands and basically adding the logic for parsing such commands in Ruff. SyntaxThe linked issue (#8094) provides a real world example for a false positive. I've narrowed it down to the following lines: versions = {'Latest': 1, 'v7.2-112': 2, 'v6': 3}
# ^^^^^^ F841 unused variable
!sed -i '/^GPUOWL_PRP_PM1=/c\GPUOWL_PRP_PM1={versions[gpuowl_prp_pm1]}' '{file}'
# ^^^^^^^^ used here Here, the warning we raise is that the That said, if we do decide to parse the escape commands, the following would be required:
Even in the case of (2), we would further need to add an ability to parse arguments for the shell commands (see the above |
%time
magic not supported
(Converting this into a general issue to discuss parsing IPython escape commands.) |
I am not sure if Ruff would actually need a full shell parser, as least not for that example from my notebook. Ruff would only need to look for matching pairs of curly brackets and check if there is valid Python syntax inside. This would need to be done before running a shell parser anyway, as it could be invalid Bash syntax, especially if it is unquoted. Jupyter seems to be smart enough to silently ignore any curly brackets with invalid Python syntax inside, such as with Bash brace expansion or an Supporting my first example in #8094 likely would require a shell parser, but that would only affect a single Ruff rule ( |
The simplest starting point for cell magics would be to mark some cell magics as transparent (possibly a customizable list to allow third party magics to be added?). That would allow this:
to no longer mark y as unused. It's only a first order fix - I think these are the built-in transparent cell magics (based on looking at the details given by
Supporting inline magics seems like a second order issue, and a tricker one, since the arguments are inline with the body. And you can rewrite the above example into a cell magic easily. |
Also, this is exactly how Black works, I think: https://black.readthedocs.io/en/latest/usage_and_configuration/the_basics.html#python-cell-magics It seems to have a pre-built list of magics and that can be extended. |
I'm very interested in supporting this! |
Correct me if I'm wrong but by transparent, I'm assuming you mean that the variables / imports defined in that cell are available in the global scope to be accessed by other code in other cells.
I see what you mean here. I think this should be trivial to implement. We would just need to update the ruff/crates/ruff_notebook/src/cell.rs Lines 62 to 63 in ec7456b
For example, taking the above code: Module(
ModModule {
range: 0..20,
body: [
Assign(
StmtAssign { .. }, // y = 1
),
IpyEscapeCommand(
StmtIpyEscapeCommand {
range: 6..13,
kind: Magic2,
value: "timeit",
},
),
Assign(
StmtAssign { .. }, // x = y
),
],
},
) This means we wouldn't mark |
## Summary This PR updates the logic for `is_magic_cell` to include certain cell magics. These cell magics would contain Python code following the line defining the command. The code could define a variable which can then be referenced in other cells. Currently, we would ignore the cell completely leading to undefined-name violation. As discussed in #8354 (comment) ## Test Plan Add new test case to validate this scenario.
I believe we now support these transparent / simple cases, which is probably the best we can do. |
With ruff 0.1.3, running
ruff check test.ipynb
on a notebook whose content is:returns:
While I would expect no error to be found. It seems the IPython
%time
magic isn't supported. I haven't tested other magics.The text was updated successfully, but these errors were encountered: