Skip to content

Commit

Permalink
skip if all magics (#705)
Browse files Browse the repository at this point in the history
* skip if all magics

* only check for lines with text
  • Loading branch information
MarcoGorelli committed Mar 5, 2022
1 parent f2c53d4 commit 8f1cf1b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/requirements-docs.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
myst-parser>=0.12.0
myst-parser==0.16.1 # todo: unpin
Sphinx>=3.2.0
sphinx-copybutton>=0.3.0
sphinx-rtd-theme>=0.5.0
7 changes: 7 additions & 0 deletions nbqa/save_code_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ def _should_ignore_code_cell(
or any(magic in joined_source for magic in TRANSFORMED_MAGICS)
):
return True
if all(
any(line.startswith(symbol) for symbol in ("%", "?", "!"))
for line in source
if line.strip()
):
# It's all magic, nothing to process
return True
try:
ast.parse(joined_source)
except SyntaxError:
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
autoflake
autopep8
black
black==21.12b0 # todo: unpin
blacken-docs
coverage[toml]
flake8
Expand Down
53 changes: 53 additions & 0 deletions tests/data/all_magic_cell.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"skip-flake8"
]
},
"outputs": [],
"source": [
"%load_ext nb_black\n",
"\n",
"%matplotlib inline\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"\n",
"sys.version"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
17 changes: 17 additions & 0 deletions tests/tools/test_flake8_works.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,20 @@ def test_flake8_works(
expected_err = ""
assert sorted(out.splitlines()) == sorted(expected_out.splitlines())
assert sorted(err.splitlines()) == sorted(expected_err.splitlines())


def test_cell_with_all_magics(capsys: "CaptureFixture") -> None:
"""
Should ignore cell with all magics.
Parameters
----------
capsys
Pytest fixture to capture stdout and stderr.
"""
path = os.path.join("tests", "data", "all_magic_cell.ipynb")
main(["flake8", path])

out, err = capsys.readouterr()
assert out == ""
assert err == ""

0 comments on commit 8f1cf1b

Please sign in to comment.