mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 18:02:58 +00:00
Allow transparent cell magics (#8911)
## 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 https://github.com/astral-sh/ruff/issues/8354#issuecomment-1832221009 ## Test Plan Add new test case to validate this scenario.
This commit is contained in:
parent
04ec11a73d
commit
6bbabceead
7 changed files with 102 additions and 4 deletions
|
@ -4,5 +4,10 @@
|
|||
"id": "1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": ["%%timeit\n", "print('hello world')"]
|
||||
"source": [
|
||||
"%%script bash\n",
|
||||
"for i in 1 2 3; do\n",
|
||||
" echo $i\n",
|
||||
"done"
|
||||
]
|
||||
}
|
||||
|
|
11
crates/ruff_notebook/resources/test/fixtures/jupyter/cell/valid_cell_magic.json
vendored
Normal file
11
crates/ruff_notebook/resources/test/fixtures/jupyter/cell/valid_cell_magic.json
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"execution_count": null,
|
||||
"cell_type": "code",
|
||||
"id": "1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%timeit\n",
|
||||
"print('hello world')"
|
||||
]
|
||||
}
|
|
@ -26,6 +26,18 @@
|
|||
"%%timeit\n",
|
||||
"import sys"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "36dedfd1-6c03-4894-bea6-6c1687b82b3c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%random\n",
|
||||
"# This cell is ignored\n",
|
||||
"import pathlib"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
@ -22,8 +22,19 @@
|
|||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%timeit\n",
|
||||
"import sys"
|
||||
"%%timeit"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4b6d7faa-72b3-4087-8670-fe6d35e41fb6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%random\n",
|
||||
"# This cell is ignored\n",
|
||||
"import pathlib"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
|
@ -170,7 +170,50 @@ impl Cell {
|
|||
}
|
||||
|
||||
// Detect cell magics (which operate on multiple lines).
|
||||
lines.any(|line| line.trim_start().starts_with("%%"))
|
||||
lines.any(|line| {
|
||||
line.split_whitespace().next().is_some_and(|first| {
|
||||
if first.len() < 2 {
|
||||
return false;
|
||||
}
|
||||
let (token, command) = first.split_at(2);
|
||||
// These cell magics are special in that the lines following them are valid
|
||||
// Python code and the variables defined in that scope are available to the
|
||||
// rest of the notebook.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// Cell 1:
|
||||
// ```python
|
||||
// x = 1
|
||||
// ```
|
||||
//
|
||||
// Cell 2:
|
||||
// ```python
|
||||
// %%time
|
||||
// y = x
|
||||
// ```
|
||||
//
|
||||
// Cell 3:
|
||||
// ```python
|
||||
// print(y) # Here, `y` is available.
|
||||
// ```
|
||||
//
|
||||
// This is to avoid false positives when these variables are referenced
|
||||
// elsewhere in the notebook.
|
||||
token == "%%"
|
||||
&& !matches!(
|
||||
command,
|
||||
"capture"
|
||||
| "debug"
|
||||
| "prun"
|
||||
| "pypy"
|
||||
| "python"
|
||||
| "python3"
|
||||
| "time"
|
||||
| "timeit"
|
||||
)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -426,6 +426,7 @@ mod tests {
|
|||
#[test_case(Path::new("code_and_magic.json"), true; "code_and_magic")]
|
||||
#[test_case(Path::new("only_code.json"), true; "only_code")]
|
||||
#[test_case(Path::new("cell_magic.json"), false; "cell_magic")]
|
||||
#[test_case(Path::new("valid_cell_magic.json"), true; "valid_cell_magic")]
|
||||
#[test_case(Path::new("automagic.json"), false; "automagic")]
|
||||
#[test_case(Path::new("automagics.json"), false; "automagics")]
|
||||
#[test_case(Path::new("automagic_before_code.json"), false; "automagic_before_code")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue