Repository: mwouts/jupytext
Pull request #593 (2020-08-21 07:51:20)

Jupytext --to file warning
 `jupytext notebook.ipynb --to filename.py` will warn that `--to` is used in place of `--output`.

codecov[bot]: # [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=h1) Report
> Merging [#593](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=desc) into [master](https://codecov.io/gh/mwouts/jupytext/commit/b8c2a1c774db1bd24627c54019421f99ff1de4d6?el=desc) will **decrease** coverage by `3.73%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/mwouts/jupytext/pull/593/graphs/tree.svg?width=650&height=150&src=pr&token=a1pNjsiILi)](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #593      +/-   ##
==========================================
- Coverage   99.01%   95.28%   -3.74%     
==========================================
  Files          90       90              
  Lines        8933     8948      +15     
==========================================
- Hits         8845     8526     -319     
- Misses         88      422     +334     
```


| [Impacted Files](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [jupytext/cli.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-anVweXRleHQvY2xpLnB5) | `96.80% <100.00%> (+0.05%)` | :arrow_up: |
| [tests/test\_cli.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9jbGkucHk=) | `92.91% <100.00%> (-7.09%)` | :arrow_down: |
| [jupytext/myst.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-anVweXRleHQvbXlzdC5weQ==) | `22.79% <0.00%> (-74.62%)` | :arrow_down: |
| [tests/test\_using\_cli.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF91c2luZ19jbGkucHk=) | `30.76% <0.00%> (-69.24%)` | :arrow_down: |
| [tests/test\_isort.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9pc29ydC5weQ==) | `50.00% <0.00%> (-50.00%)` | :arrow_down: |
| [tests/test\_read\_simple\_pandoc.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9wYW5kb2MucHk=) | `57.89% <0.00%> (-42.11%)` | :arrow_down: |
| [jupytext/pandoc.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-anVweXRleHQvcGFuZG9jLnB5) | `62.06% <0.00%> (-34.49%)` | :arrow_down: |
| [tests/test\_ipynb\_to\_myst.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9pcHluYl90b19teXN0LnB5) | `67.69% <0.00%> (-32.31%)` | :arrow_down: |
| [tests/test\_pre\_commit.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9wcmVfY29tbWl0LnB5) | `90.25% <0.00%> (-9.75%)` | :arrow_down: |
| [tests/test\_formats.py](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9mb3JtYXRzLnB5) | `91.66% <0.00%> (-8.34%)` | :arrow_down: |
| ... and [5 more](https://codecov.io/gh/mwouts/jupytext/pull/593/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=footer). Last update [b8c2a1c...3fc8482](https://codecov.io/gh/mwouts/jupytext/pull/593?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 16149847..b633f7a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
 - Activated GitHub code scanning alerts
 - New option `hide_notebook_metadata` to encapsulate the notebook metadata in an HTML comment (#527)
 - Tested `isort` on notebooks (#553)
+- `jupytext notebook.ipynb --to filename.py` will warn that `--to` is used in place of `--output`.
 
 **Changed**
 - Install Jupytext from source on MyBinder to avoid cache issues (#567)
diff --git a/jupytext/cli.py b/jupytext/cli.py
index 5a72848e..3312d5e0 100644
--- a/jupytext/cli.py
+++ b/jupytext/cli.py
@@ -8,6 +8,7 @@ import shlex
 import subprocess
 import argparse
 import json
+import warnings
 from copy import copy
 from tempfile import NamedTemporaryFile
 from .jupytext import read, reads, write, writes
@@ -352,6 +353,24 @@ def jupytext(args=None):
     if args.output and len(args.notebooks) != 1:
         raise ValueError("Please input a single notebook when using --output")
 
+    # Warn if '--to' is used in place of '--output'
+    if (
+        not args.output
+        and args.output_format
+        and "." in args.output_format
+        and not args.output_format.startswith(".")
+        and "//" not in args.output_format
+    ):
+
+        def single_line(msg, *args, **kwargs):
+            return "[warning] {}\n".format(msg)
+
+        warnings.formatwarning = single_line
+        warnings.warn(
+            "You have passed a file name to the '--to' option, "
+            "when a format description was expected. Maybe you want to use the '-o' option instead?"
+        )
+
     if args.input_format:
         args.input_format = long_form_one_format(args.input_format)
 


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index ed5e0eaf..3169c0e0 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1120,3 +1120,23 @@ def test_sync_script_dotdot_folder_564(tmpdir):
 
     jupytext(["--sync", str(py_file)])
     jupytext(["--sync", str(nb_file)])
+
+
+def test_jupytext_to_file_emits_a_warning(tmpdir):
+    """The user may type
+        jupytext notebook.ipynb --to script.py
+    meaning
+        jupytext notebook.ipynb -o script.py
+    """
+    os.chdir(str(tmpdir))
+
+    nb_file = tmpdir.join("notebook.ipynb")
+    write(new_notebook(), str(nb_file))
+
+    with pytest.warns(None) as record:
+        jupytext(["notebook.ipynb", "-o", "script.py"])
+
+    assert len(record) == 0
+
+    with pytest.warns(UserWarning, match="Maybe you want to use the '-o' option"):
+        jupytext(["notebook.ipynb", "--to", "script.py"])