From 4a474ea72c61c4ffb7ef4051181f85e3a820b18a Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Mon, 15 Mar 2021 07:13:18 +0100 Subject: [PATCH] Settings from the config file have precedence over those in the notebook at save time --- jupytext/jupytext.py | 19 ++++++------------- tests/test_contentsmanager.py | 14 +++++++++----- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/jupytext/jupytext.py b/jupytext/jupytext.py index 77c37b63f..aecd38afb 100644 --- a/jupytext/jupytext.py +++ b/jupytext/jupytext.py @@ -59,21 +59,14 @@ def __init__(self, fmt, config): def update_fmt_with_notebook_options(self, metadata, read=False): """Update format options with the values in the notebook metadata, and record those options in the notebook metadata""" + # The settings in the Jupytext configuration file have precedence over the metadata in the notebook + # when the notebook is saved. This is because the metadata in the notebook might not be visible + # in the text representation when e.g. notebook_metadata_filter="-all", which makes them hard to edit. + if not read and self.config is not None: + self.config.set_default_format_options(self.fmt, read) + # format options in notebook have precedence over that in fmt, and precedence over the config for opt in _VALID_FORMAT_OPTIONS: - if self.config is not None: - # We use the config filters if provided - if ( - opt == "notebook_metadata_filter" - and self.config.default_notebook_metadata_filter - ): - continue - if ( - opt == "cell_metadata_filter" - and self.config.default_cell_metadata_filter - ): - continue - if opt in metadata.get("jupytext", {}): self.fmt.setdefault(opt, metadata["jupytext"][opt]) diff --git a/tests/test_contentsmanager.py b/tests/test_contentsmanager.py index 14eba5e23..d4cdbeb5c 100644 --- a/tests/test_contentsmanager.py +++ b/tests/test_contentsmanager.py @@ -1541,21 +1541,25 @@ def test_save_file_with_default_cell_markers(tmpdir): nb = cm.get("nb.py")["content"] assert len(nb.cells) == 1 - nb.metadata["jupytext"]["cell_markers"] = "+,-" - del nb.metadata["jupytext"]["notebook_metadata_filter"] cm.save(model=notebook_model(nb), path="nb.py") with open(tmp_py) as fp: text2 = fp.read() compare( - "\n".join(text2.splitlines()[-len(text.splitlines()) :]), - "\n".join(text.splitlines()), + text2, + """# region +# this is a unique code cell +1 + 1 + +2 + 2 +# endregion +""", ) nb2 = cm.get("nb.py")["content"] compare_notebooks(nb2, nb) - assert nb2.metadata["jupytext"]["cell_markers"] == "+,-" + assert nb2.metadata["jupytext"]["cell_markers"] == "region,endregion" def test_notebook_extensions(tmpdir):