Skip to content

Commit

Permalink
ENH: allow for flexible order (#76)
Browse files Browse the repository at this point in the history
* allow for flexible order

* clarify readme

* typo
  • Loading branch information
MarcoGorelli committed Jul 18, 2020
1 parent e0c6c13 commit 9686e78
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 23 deletions.
20 changes: 10 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ The general syntax is

.. code-block:: bash
nbqa <command> <notebook or directory> <flags>
nbqa -c <command> <args>
For example, you could run:
, where :code:`command` is any Python code quality tool. For example, you could run:

.. code-block:: bash
$ nbqa flake8 my_notebook.ipynb
$ nbqa black my_notebook.ipynb --check
$ nbqa mypy my_notebook.ipynb --ignore-missing-imports
$ nbqa pytest my_notebook.ipynb --doctest-modules
$ nbqa -c flake8 my_notebook.ipynb
$ nbqa -c black my_notebook.ipynb --check
$ nbqa -c mypy my_notebook.ipynb --ignore-missing-imports
$ nbqa -c pytest my_notebook.ipynb --doctest-modules
You can also pass an entire directory instead of a single file, e.g. :code:`nbqa flake8 my_notebooks`.
You can also pass an entire directory instead of a single file, e.g. :code:`nbqa -c flake8 my_notebooks`.

Examples
--------
Expand All @@ -73,7 +73,7 @@ Format your notebooks using :code:`black`:

.. code-block:: bash
$ nbqa black .
$ nbqa -c black .
reformatted tweet-sentiment-roberta-pytorch.ipynb
All done! ✨ 🍰 ✨
1 files reformatted.
Expand All @@ -82,14 +82,14 @@ Check static type annotations:

.. code-block:: bash
$ nbqa mypy tweet-sentiment-roberta-pytorch.ipynb --ignore-missing-imports
$ nbqa -c mypy tweet-sentiment-roberta-pytorch.ipynb --ignore-missing-imports
tweet-sentiment-roberta-pytorch.ipynb:cell_10:5: error: Argument "batch_size" to "get_test_loader" has incompatible type "str"; expected "int"
Check any examples in your docstrings are correct:

.. code-block:: bash
$ nbqa pytest tweet-sentiment-roberta-pytorch.ipynb --doctest-modules
$ nbqa -c pytest tweet-sentiment-roberta-pytorch.ipynb --doctest-modules
============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /home/marco/tweet-sentiment-extraction
Expand Down
2 changes: 1 addition & 1 deletion nbqa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _parse_args(raw_args):
description="Adapter to run any code-quality tool on a Jupyter notebook.",
usage="nbqa <command> <notebook or directory> <flags>",
)
parser.add_argument("command", help="Command to run, e.g. `flake8`.")
parser.add_argument("-c", "--command", help="Command to run, e.g. `flake8`.")
parser.add_argument(
"root_dir", default=".", nargs="?", help="File or directory to run command on."
)
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test_isort_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_isort_works(tmp_notebook_for_testing, capsys):
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["isort", "tests/data/notebook_for_testing.ipynb"])
main(["--command=isort", "tests/data/notebook_for_testing.ipynb"])

with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_black_works(tmp_notebook_for_testing, capsys):
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["black", "tests/data/notebook_for_testing.ipynb"])
main(["--command=black", "tests/data/notebook_for_testing.ipynb"])
with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_pytest_doctest_works(tmp_notebook_for_testing, capsys):
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["pytest", "--doctest-modules"])
main(["--command=pytest", "--doctest-modules"])

with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_flake8_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_flake8_works(tmp_notebook_for_testing, capsys):
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["flake8"])
main(["--command=flake8"])

with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_isort_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_isort_works(tmp_notebook_for_testing, capsys):
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["isort", "tests/data/notebook_for_testing.ipynb"])
main(["--command=isort", "tests/data/notebook_for_testing.ipynb"])

with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()
Expand All @@ -38,7 +38,7 @@ def test_isort_initial_md(tmp_notebook_starting_with_md, capsys):
with open(tmp_notebook_starting_with_md, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["isort", "tests/data/notebook_starting_with_md.ipynb"])
main(["--command=isort", "tests/data/notebook_starting_with_md.ipynb"])

with open(tmp_notebook_starting_with_md, "r") as handle:
after = handle.readlines()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_missing_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def test_missing_command():
"Please make sure you have it installed before running nbQA on it."
)
with pytest.raises(ValueError, match=msg):
main([command, "--some-flag"])
main([f"--command={command}", "--some-flag"])
2 changes: 1 addition & 1 deletion tests/test_mypy_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_mypy_works(tmp_notebook_for_testing, capsys):
with open(tmp_notebook_for_testing, "r") as handle:
before = handle.readlines()
with pytest.raises(SystemExit):
main(["mypy", "--ignore-missing-imports"])
main(["--command=mypy", "--ignore-missing-imports"])

with open(tmp_notebook_for_testing, "r") as handle:
after = handle.readlines()
Expand Down
31 changes: 27 additions & 4 deletions tests/test_return_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,28 @@

def test_flake8_return_code():
output = subprocess.run(
["python", "-m", "nbqa", "flake8", "tests/data/notebook_for_testing.ipynb"]
[
"python",
"-m",
"nbqa",
"--command",
"flake8",
"tests/data/notebook_for_testing.ipynb",
]
)
result = output.returncode
expected = 1
assert result == expected

output = subprocess.run(
["python", "-m", "nbqa", "flake8", "tests/data/clean_notebook.ipynb"]
[
"python",
"-m",
"nbqa",
"--command",
"flake8",
"tests/data/clean_notebook.ipynb",
]
)
result = output.returncode
expected = 0
Expand All @@ -33,13 +47,21 @@ def test_black_return_code():
assert result == expected

output = subprocess.run(
["python", "-m", "nbqa", "black", "tests/data/clean_notebook.ipynb", "--check"]
[
"python",
"-m",
"nbqa",
"-c",
"black",
"--check",
"tests/data/clean_notebook.ipynb",
]
)
result = output.returncode
expected = 0
assert result == expected

output = subprocess.run(["python", "-m", "nbqa", "black", "--check"])
output = subprocess.run(["python", "-m", "nbqa", "--command", "black", "--check"])
result = output.returncode
expected = 1
assert result == expected
Expand All @@ -49,6 +71,7 @@ def test_black_return_code():
"python",
"-m",
"nbqa",
"--command",
"black",
"tests/data/clean_notebook.ipynb",
"--check",
Expand Down

0 comments on commit 9686e78

Please sign in to comment.