Repository: mwouts/jupytext
Pull request #195 (2019-02-27 21:22:00)

1.0.2


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index e0c0218e..db4c9d09 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -3,6 +3,13 @@
 Release History
 ---------------
 
+1.0.2 (2019-02-??)
+++++++++++++++++++++++
+
+**BugFixes**
+- Allow spaces between ``?`` or ``!`` and python or bash command (#189)
+
+
 1.0.1 (2019-02-23)
 ++++++++++++++++++++++
 
diff --git a/jupytext/contentsmanager.py b/jupytext/contentsmanager.py
index e637df55..1020fa3e 100644
--- a/jupytext/contentsmanager.py
+++ b/jupytext/contentsmanager.py
@@ -413,6 +413,14 @@ class TextFileContentsManager(FileContentsManager, Configurable):
 
     def rename_file(self, old_path, new_path):
         """Rename the current notebook, as well as its alternative representations"""
+        if old_path not in self.paired_notebooks:
+            try:
+                # we do not know yet if this is a paired notebook (#190)
+                # -> to get this information we open the notebook
+                self.get(old_path, content=True)
+            except Exception:
+                pass
+
         if old_path not in self.paired_notebooks:
             super(TextFileContentsManager, self).rename_file(old_path, new_path)
             return
diff --git a/jupytext/magics.py b/jupytext/magics.py
index e089abae..3704673f 100644
--- a/jupytext/magics.py
+++ b/jupytext/magics.py
@@ -16,7 +16,7 @@ _MAGIC_NOT_ESC_RE = {_SCRIPT_EXTENSIONS[ext]['language']: re.compile(
 _COMMENT = {_SCRIPT_EXTENSIONS[ext]['language']: _SCRIPT_EXTENSIONS[ext]['comment'] for ext in _SCRIPT_EXTENSIONS}
 
 # Commands starting with a question or exclamation mark have to be escaped
-_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)[A-Za-z]")
+_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)\s*[A-Za-z]")
 
 _PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})(\s|$)".format('|'.join(
     # posix
diff --git a/jupytext/version.py b/jupytext/version.py
index 8e49dc8e..86b6eea1 100644
--- a/jupytext/version.py
+++ b/jupytext/version.py
@@ -1,3 +1,3 @@
 """Jupytext's version number"""
 
-__version__ = '1.0.1'
+__version__ = '1.0.2-dev'


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 6ef6af71..564a118b 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -15,7 +15,7 @@ from jupytext.cli import parse_jupytext_args, jupytext, jupytext_cli, system, st
 from jupytext.compare import compare_notebooks
 from jupytext.paired_paths import paired_paths
 from jupytext.formats import long_form_one_format, JupytextFormatError
-from .utils import list_notebooks, requires_black, requires_flake8
+from .utils import list_notebooks, requires_black, requires_flake8, skip_if_dict_is_not_ordered
 
 
 def test_str2bool():
@@ -304,6 +304,7 @@ def test_convert_to_percent_format_and_keep_magics(nb_file, tmpdir):
     compare_notebooks(nb1, nb2)
 
 
+@skip_if_dict_is_not_ordered
 def test_pre_commit_hook(tmpdir):
     tmp_ipynb = str(tmpdir.join('nb with spaces.ipynb'))
     tmp_py = str(tmpdir.join('nb with spaces.py'))
@@ -334,6 +335,7 @@ def test_pre_commit_hook(tmpdir):
     assert os.path.isfile(tmp_py)
 
 
+@skip_if_dict_is_not_ordered
 def test_pre_commit_hook_py_to_ipynb_and_md(tmpdir):
     tmp_ipynb = str(tmpdir.join('nb with spaces.ipynb'))
     tmp_py = str(tmpdir.join('nb with spaces.py'))
diff --git a/tests/test_escape_magics.py b/tests/test_escape_magics.py
index 77fb01d7..190f9dc8 100644
--- a/tests/test_escape_magics.py
+++ b/tests/test_escape_magics.py
@@ -107,22 +107,22 @@ def test_force_comment_using_contents_manager(tmpdir):
         assert '%pylab inline' in stream.read().splitlines()
 
 
-@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b'])
+@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b', '! mkdir tmp'])
 def test_comment_bash_commands_in_python(magic_cmd):
-    comment_magic([magic_cmd]) == ['# ' + magic_cmd]
-    uncomment_magic(['# ' + magic_cmd]) == magic_cmd
+    assert comment_magic([magic_cmd]) == ['# ' + magic_cmd]
+    assert uncomment_magic(['# ' + magic_cmd]) == [magic_cmd]
 
 
 @pytest.mark.parametrize('not_magic_cmd', ['copy(a)', 'copy.deepcopy'])
 def test_do_not_comment_python_cmds(not_magic_cmd):
-    comment_magic([not_magic_cmd]) == [not_magic_cmd]
-    uncomment_magic([not_magic_cmd]) == not_magic_cmd
+    assert comment_magic([not_magic_cmd]) == [not_magic_cmd]
+    assert uncomment_magic([not_magic_cmd]) == [not_magic_cmd]
 
 
 @pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b'])
 def test_do_not_comment_bash_commands_in_R(magic_cmd):
-    comment_magic([magic_cmd], language='R') == ['# ' + magic_cmd]
-    uncomment_magic(['# ' + magic_cmd], language='R') == magic_cmd
+    assert comment_magic([magic_cmd], language='R') == [magic_cmd]
+    assert uncomment_magic([magic_cmd], language='R') == [magic_cmd]
 
 
 def test_markdown_image_is_not_magic():
diff --git a/tests/test_read_simple_percent.py b/tests/test_read_simple_percent.py
index 814c6511..b16de277 100644
--- a/tests/test_read_simple_percent.py
+++ b/tests/test_read_simple_percent.py
@@ -189,3 +189,20 @@ def test_multiple_empty_cells():
     nb2 = jupytext.reads(text, 'py:percent')
     nb2.metadata = nb.metadata
     compare(nb, nb2)
+
+
+def test_first_cell_markdown_191():
+    text = """# %% [markdown]
+# Docstring
+
+# %%
+from math import pi
+
+# %% [markdown]
+# Another markdown cell
+"""
+
+    nb = jupytext.reads(text, 'py')
+    assert nb.cells[0].cell_type == 'markdown'
+    assert nb.cells[1].cell_type == 'code'
+    assert nb.cells[2].cell_type == 'markdown'