From 1732c2f63c2dff46515d4ee97e33825d3e2870b9 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Fri, 9 Jul 2021 00:29:38 +0200 Subject: [PATCH] Reproduce and fix the issue with notebooks paired with scripts in a folder --- docs/CHANGELOG.md | 5 ++++- jupytext/paired_paths.py | 24 +++++++++++++++++++++-- tests/test_paired_paths.py | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d3a336593..77257c5d0 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,9 +1,12 @@ Jupytext ChangeLog ================== -1.11.4 (2021-07-??) +1.11.4 (2021-07-09) ------------------- +**Fixed** +- Fixed an `InconsistentPath` issue with notebooks paired with scripts in a folder ([#806](https://github.com/mwouts/jupytext/issues/806)) + **Changed** - (Documentation) the `cell_markers` option (and the other ones) can be set directly in the `jupytext.toml` config file ([#809](https://github.com/mwouts/jupytext/issues/809)). diff --git a/jupytext/paired_paths.py b/jupytext/paired_paths.py index f9b40323c..2475e4ce1 100644 --- a/jupytext/paired_paths.py +++ b/jupytext/paired_paths.py @@ -232,11 +232,31 @@ def paired_paths(main_path, fmt, formats): if not formats: return [(main_path, {"extension": os.path.splitext(main_path)[1]})] + if fmt is None: + fmt = {"extension": os.path.splitext(main_path)[1]} + + fmt = long_form_one_format(fmt) formats = long_form_multiple_formats(formats) + paths = [] # Is there a format that matches the main path? - base = base_path(main_path, fmt) - paths = [full_path(base, f) for f in formats] + for f in formats: + if f["extension"] != fmt["extension"]: + continue + if ( + "format_name" in fmt + and "format_name" in f + and f["format_name"] != fmt["format_name"] + ): + continue + # extend 'fmt' with the format information (prefix, suffix) from f + extended_fmt = {key: fmt.get(key, value) for key, value in f.items()} + + base = base_path(main_path, extended_fmt) + paths = [full_path(base, f) for f in formats] + + if main_path in paths: + break if main_path not in paths: raise InconsistentPath( diff --git a/tests/test_paired_paths.py b/tests/test_paired_paths.py index 172ff360b..a57a6cc6b 100644 --- a/tests/test_paired_paths.py +++ b/tests/test_paired_paths.py @@ -3,6 +3,8 @@ import pytest +import jupytext +from jupytext.cli import jupytext as jupytext_cli from jupytext.compare import compare from jupytext.contentsmanager import TextFileContentsManager from jupytext.formats import ( @@ -247,3 +249,40 @@ def test_cm_paired_paths(): zero = "" cm.update_paired_notebooks("nb.ipynb", zero) assert cm.paired_notebooks == {} + + +def test_paired_path_with_prefix( + nb_file="scripts/test.py", + fmt={"extension": ".py", "format_name": "percent"}, + formats=[ + {"extension": ".ipynb"}, + {"prefix": "scripts/", "format_name": "percent", "extension": ".py"}, + ], +): + assert paired_paths(nb_file, fmt, formats) == [ + ("test.ipynb", {"extension": ".ipynb"}), + ( + "scripts/test.py", + {"prefix": "scripts/", "format_name": "percent", "extension": ".py"}, + ), + ] + + +def test_paired_notebook_ipynb_root_scripts_in_folder_806( + tmpdir, cwd_tmpdir, python_notebook +): + test_ipynb = tmpdir / "test.ipynb" + jupytext.write(python_notebook, str(test_ipynb)) + jupytext_cli(["--set-formats", "ipynb,scripts//py:percent", "test.ipynb"]) + assert (tmpdir / "scripts" / "test.py").exists() + test_ipynb.remove() + jupytext_cli( + [ + "--to", + "notebook", + "--output", + "test.ipynb", + "scripts/test.py", + ] + ) + assert test_ipynb.exists()