Skip to content

Commit

Permalink
Fix magic methods failing to be unmasked
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676048146
  • Loading branch information
AleksMat authored and copybara-github committed Sep 19, 2024
1 parent 8dfd235 commit fc212b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
28 changes: 27 additions & 1 deletion patches/pyink.patch
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@
(917760, 917999, 0),
-]
+)
--- a/handle_ipynb_magics.py
+++ b/handle_ipynb_magics.py
@@ -263,6 +263,8 @@
"""
for replacement in replacements:
src = src.replace(replacement.mask, replacement.src)
+ # Strings in src might have been reformatted with single quotes.
+ src = src.replace(f"'{replacement.mask[1:-1]}'", replacement.src)
return src


--- a/linegen.py
+++ b/linegen.py
@@ -9,6 +9,11 @@ from enum import Enum, auto
Expand Down Expand Up @@ -1398,6 +1409,14 @@
) -> None:
--- a/tests/test_ipynb.py
+++ b/tests/test_ipynb.py
@@ -12,6 +12,7 @@
from pyink import (
Mode,
NothingChanged,
+ QuoteStyle,
format_cell,
format_file_contents,
format_file_in_place,
@@ -26,8 +26,10 @@
pytest.importorskip("tokenize_rt", reason="tokenize-rt is an optional dependency")

Expand All @@ -1409,7 +1428,7 @@

runner = CliRunner()

@@ -209,6 +209,22 @@
@@ -209,6 +209,29 @@
assert result == expected_output


Expand All @@ -1428,6 +1447,13 @@
+ with pytest.raises(NothingChanged):
+ format_cell(src, fast=True, mode=JUPYTER_MODE)
+
+
+def test_cell_magic_with_forced_single_quoted_strings() -> None:
+ src = "%time"
+ mode = replace(JUPYTER_MODE, quote_style=QuoteStyle.SINGLE)
+ with pytest.raises(NothingChanged):
+ format_cell(src, fast=True, mode=mode)
+
+
def test_cell_magic_nested() -> None:
src = "%%time\n%%time\n2+2"
Expand Down
2 changes: 2 additions & 0 deletions src/pyink/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ def unmask_cell(src: str, replacements: List[Replacement]) -> str:
"""
for replacement in replacements:
src = src.replace(replacement.mask, replacement.src)
# Strings in src might have been reformatted with single quotes.
src = src.replace(f"'{replacement.mask[1:-1]}'", replacement.src)
return src


Expand Down
8 changes: 8 additions & 0 deletions tests/test_ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pyink import (
Mode,
NothingChanged,
QuoteStyle,
format_cell,
format_file_contents,
format_file_in_place,
Expand Down Expand Up @@ -226,6 +227,13 @@ def test_cell_magic_with_custom_python_magic_after_spaces_and_comments_noop(
format_cell(src, fast=True, mode=JUPYTER_MODE)


def test_cell_magic_with_forced_single_quoted_strings() -> None:
src = "%time"
mode = replace(JUPYTER_MODE, quote_style=QuoteStyle.SINGLE)
with pytest.raises(NothingChanged):
format_cell(src, fast=True, mode=mode)


def test_cell_magic_nested() -> None:
src = "%%time\n%%time\n2+2"
result = format_cell(src, fast=True, mode=JUPYTER_MODE)
Expand Down

0 comments on commit fc212b9

Please sign in to comment.