From 7d142954fd9ba02ef4f794bbd56dc27fc054e527 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 15 Dec 2023 10:16:00 +0100 Subject: [PATCH 1/3] Fix #9145 --- .../jupyter/cell/unicode_magic_gh9145.json | 14 ++++ crates/ruff_notebook/src/cell.rs | 79 +++++++++---------- crates/ruff_notebook/src/notebook.rs | 1 + 3 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json diff --git a/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json b/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json new file mode 100644 index 0000000000000..af458c9a9e642 --- /dev/null +++ b/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json @@ -0,0 +1,14 @@ +{ + "execution_count": null, + "cell_type": "code", + "id": "1", + "metadata": {}, + "outputs": [], + "source": [ + "def sample_func(xx):\n", + " \"\"\"\n", + " 转置 (transpose)\n", + " \"\"\"\n", + " return xx.T" + ] +} diff --git a/crates/ruff_notebook/src/cell.rs b/crates/ruff_notebook/src/cell.rs index caa4cb3204a03..eefc18918b5cb 100644 --- a/crates/ruff_notebook/src/cell.rs +++ b/crates/ruff_notebook/src/cell.rs @@ -171,48 +171,43 @@ impl Cell { // Detect cell magics (which operate on multiple lines). 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" - ) - }) + let Some(first) = line.split_whitespace().next() else { + return false; + }; + if first.len() < 2 { + return false; + } + let Some(command) = first.strip_prefix("%%") else { + return false; + }; + // 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. + !matches!( + command, + "capture" | "debug" | "prun" | "pypy" | "python" | "python3" | "time" | "timeit" + ) }) } } diff --git a/crates/ruff_notebook/src/notebook.rs b/crates/ruff_notebook/src/notebook.rs index 7c9e8356b79d3..ec79306b51841 100644 --- a/crates/ruff_notebook/src/notebook.rs +++ b/crates/ruff_notebook/src/notebook.rs @@ -431,6 +431,7 @@ mod tests { #[test_case(Path::new("automagics.json"), false; "automagics")] #[test_case(Path::new("automagic_before_code.json"), false; "automagic_before_code")] #[test_case(Path::new("automagic_after_code.json"), true; "automagic_after_code")] + #[test_case(Path::new("unicode_magic_gh9145.json"), true; "unicode_magic_gh9145")] fn test_is_valid_code_cell(path: &Path, expected: bool) -> Result<()> { /// Read a Jupyter cell from the `resources/test/fixtures/jupyter/cell` directory. fn read_jupyter_cell(path: impl AsRef) -> Result { From ca0f903d2ddc8cb906ec63e88d9bbe00565d1860 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 15 Dec 2023 10:16:59 +0100 Subject: [PATCH 2/3] Simplify test logic --- crates/ruff_notebook/src/notebook.rs | 29 +++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/ruff_notebook/src/notebook.rs b/crates/ruff_notebook/src/notebook.rs index ec79306b51841..ddc558ba21b87 100644 --- a/crates/ruff_notebook/src/notebook.rs +++ b/crates/ruff_notebook/src/notebook.rs @@ -421,18 +421,18 @@ mod tests { )); } - #[test_case(Path::new("markdown.json"), false; "markdown")] - #[test_case(Path::new("only_magic.json"), true; "only_magic")] - #[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")] - #[test_case(Path::new("automagic_after_code.json"), true; "automagic_after_code")] - #[test_case(Path::new("unicode_magic_gh9145.json"), true; "unicode_magic_gh9145")] - fn test_is_valid_code_cell(path: &Path, expected: bool) -> Result<()> { + #[test_case("markdown", false)] + #[test_case("only_magic", true)] + #[test_case("code_and_magic", true)] + #[test_case("only_code", true)] + #[test_case("cell_magic", false)] + #[test_case("valid_cell_magic", true)] + #[test_case("automagic", false)] + #[test_case("automagics", false)] + #[test_case("automagic_before_code", false)] + #[test_case("automagic_after_code", true)] + #[test_case("unicode_magic_gh9145", true)] + fn test_is_valid_code_cell(cell: &str, expected: bool) -> Result<()> { /// Read a Jupyter cell from the `resources/test/fixtures/jupyter/cell` directory. fn read_jupyter_cell(path: impl AsRef) -> Result { let path = notebook_path("cell").join(path); @@ -440,7 +440,10 @@ mod tests { Ok(serde_json::from_str(&source_code)?) } - assert_eq!(read_jupyter_cell(path)?.is_valid_code_cell(), expected); + assert_eq!( + read_jupyter_cell(format!("{cell}.json"))?.is_valid_code_cell(), + expected + ); Ok(()) } From fb125de440619ee06b6eac225354dd829ac2c4b3 Mon Sep 17 00:00:00 2001 From: konstin Date: Fri, 15 Dec 2023 10:37:09 +0100 Subject: [PATCH 3/3] Also fix https://github.com/astral-sh/ruff-vscode/issues/362 --- .../test/fixtures/jupyter/cell/unicode_magic_gh9145.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json b/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json index af458c9a9e642..38455b3c3df65 100644 --- a/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json +++ b/crates/ruff_notebook/resources/test/fixtures/jupyter/cell/unicode_magic_gh9145.json @@ -9,6 +9,11 @@ " \"\"\"\n", " 转置 (transpose)\n", " \"\"\"\n", - " return xx.T" + " return xx.T", + "# https://github.com/astral-sh/ruff-vscode/issues/362", + "DEFAULT_SYSTEM_PROMPT = (", + " \"Ты — Сайга, русскоязычный автоматический ассистент. \"", + " \"Ты разговариваешь с людьми и помогаешь им.\"", + ")" ] }