Skip to content

Commit

Permalink
Escape the comment char only in regular expressions (#864)
Browse files Browse the repository at this point in the history
* Raise on the first unexpected warning

* Ignore the DeprecationWarning about Validator.iter_errors

* Escape the comment char only in regular expressions

* Full coverage for the no_warning context manager

* Update CHANGELOG.md
  • Loading branch information
mwouts authored Oct 7, 2021
1 parent 8aa3d1f commit 186d822
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Jupytext ChangeLog
==================

1.13.1 (2021-10-07)
-------------------

**Fixed**
- The magic commands in `py:percent` scripts with no explicit format information remain commented over a round trip ([#848](https://github.com/mwouts/jupytext/issues/848))


1.13.0 (2021-09-25)
-------------------

Expand Down
5 changes: 3 additions & 2 deletions jupytext/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ def guess_format(text, ext):
# Is this a Hydrogen-like script?
# Or a Sphinx-gallery script?
if ext in _SCRIPT_EXTENSIONS:
comment = re.escape(_SCRIPT_EXTENSIONS[ext]["comment"])
unescaped_comment = _SCRIPT_EXTENSIONS[ext]["comment"]
comment = re.escape(unescaped_comment)
language = _SCRIPT_EXTENSIONS[ext]["language"]
twenty_hash_re = re.compile(r"^#( |)#{19,}\s*$")
double_percent_re = re.compile(r"^{}( %%|%%)$".format(comment))
Expand Down Expand Up @@ -345,7 +346,7 @@ def guess_format(text, ext):
):
double_percent_count += 1

if not line.startswith(comment) and is_magic(line, language):
if not line.startswith(unescaped_comment) and is_magic(line, language):
magic_command_count += 1

if twenty_hash_re.match(line) and ext == ".py":
Expand Down
2 changes: 1 addition & 1 deletion jupytext/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Jupytext's version number"""

__version__ = "1.13.0"
__version__ = "1.13.1"
11 changes: 9 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import time
import unittest.mock as mock
import warnings
from argparse import ArgumentTypeError
from contextlib import contextmanager
from io import StringIO
Expand Down Expand Up @@ -1133,11 +1134,11 @@ def test_sync_script_dotdot_folder_564(tmpdir):

@contextmanager
def no_warning():
with pytest.warns(None) as warnings:
with pytest.warns(None) as records:
yield

# There should be no warning
for record in warnings:
for record in records:
# Temporary exception for for this one, see #865
if (
"Passing a schema to Validator.iter_errors is deprecated "
Expand All @@ -1147,6 +1148,12 @@ def no_warning():
raise RuntimeError(record)


def test_no_warning():
with pytest.raises(RuntimeError, match="a sample warning"):
with no_warning():
warnings.warn("a sample warning")


def test_jupytext_to_file_emits_a_warning(tmpdir):
"""The user may type
jupytext notebook.ipynb --to script.py
Expand Down
26 changes: 26 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ def test_guess_format_percent(nb_file):
assert guess_format(stream.read(), ext=".py")[0] == "percent"


def test_guess_format_simple_percent(
nb="""# %%
print("hello world!")
""",
):
assert guess_format(nb, ext=".py")[0] == "percent"


def test_guess_format_simple_percent_with_magic(
nb="""# %%
# %time
print("hello world!")
""",
):
assert guess_format(nb, ext=".py")[0] == "percent"


def test_guess_format_simple_hydrogen_with_magic(
nb="""# %%
%time
print("hello world!")
""",
):
assert guess_format(nb, ext=".py")[0] == "hydrogen"


@pytest.mark.parametrize("nb_file", list_notebooks("sphinx"))
def test_guess_format_sphinx(nb_file):
with open(nb_file) as stream:
Expand Down

0 comments on commit 186d822

Please sign in to comment.