Skip to content

Commit aa9e25d

Browse files
authored
add nbqa_ipynb to temporary files (#870)
allows config globs to apply to nbqa-generated files. e.g. default_magicgr4dg5bw_nbqa_ipynb.py Added to suffix, so e.g. all generated .py files match *nbqa_ipynb.py
1 parent 07400b0 commit aa9e25d

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

docs/known-limitations.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ then the following will still not be processed:
1111
- cells with code which ``IPython`` would transform magics into (e.g. ``get_ipython().system('ls')``).
1212

1313
Because ``nbQA`` converts the code cells in Jupyter notebooks to temporary Python files for linting, certain flags like ``flake8``'s
14-
``--per-file-ignores`` don't work. The temporary Python files will not match the specified file patterns and ignored error codes will still
14+
``--per-file-ignores`` don't work perfectly.
15+
The temporary Python files will not match the specified file patterns and ignored error codes will still
1516
surface (`GH issue <https://github.com/nbQA-dev/nbQA/issues/730>`_).
17+
nbqa-generated temporary files will contain the string ``nbqa_ipynb``,
18+
so you can still apply per-file-ignores if you add an additional pattern:
19+
20+
.. sourcecode:: ini
21+
22+
[flake8]
23+
per-file-ignores =
24+
examples/*.ipynb: E402
25+
examples/*nbqa_ipynb.py: E402
26+
27+
The directory and the stem of the filename are preserved, so e.g. ``path/to/mynotebook.ipynb`` will be ``path/to/mynotebook{randomstring}_nbqa_ipynb.py`` when nbqa passes it to the linter.
1628

1729
Any other limitation is likely unintentional - if you run into any, please do report an issue.

nbqa/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def _get_nb_to_tmp_mapping(
526526
prefix=remove_suffix(
527527
os.path.basename(notebook), os.path.splitext(notebook)[-1]
528528
),
529-
suffix=SUFFIX[md],
529+
suffix="_nbqa_ipynb" + SUFFIX[md],
530530
)
531531
)
532532
relative_path, _ = get_relative_and_absolute_paths(

tests/tools/test_flake8_works.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Check :code:`flake8` works as intended."""
22

33
import os
4+
from pathlib import Path
45
from textwrap import dedent
56
from typing import TYPE_CHECKING
67

@@ -91,3 +92,49 @@ def test_cell_with_all_magics(capsys: "CaptureFixture") -> None:
9192
out, err = capsys.readouterr()
9293
assert out == ""
9394
assert err == ""
95+
96+
97+
def test_per_file_ignores(
98+
tmp_notebook_for_testing: Path, capsys: "CaptureFixture"
99+
) -> None:
100+
"""
101+
Check flake8 per-file-ignore patterns work.
102+
103+
Parameters
104+
----------
105+
tmp_notebook_for_testing
106+
notebook Path to test
107+
capsys
108+
Pytest fixture to capture stdout and stderr.
109+
"""
110+
# enable per-file ignores with nbqa glob
111+
flake8_ini = Path(".flake8")
112+
flake8_ini.write_text(
113+
dedent(
114+
"""
115+
[flake8]
116+
per-file-ignores =
117+
**/*.ipynb: E402
118+
**/*nbqa_ipynb.py: E402
119+
"""
120+
),
121+
encoding="utf-8",
122+
)
123+
124+
main(["flake8", str(tmp_notebook_for_testing)])
125+
flake8_ini.unlink()
126+
127+
expected_path_0 = os.path.join("tests", "data", "notebook_for_testing.ipynb")
128+
129+
out, err = capsys.readouterr()
130+
expected_out = dedent(
131+
f"""\
132+
{expected_path_0}:cell_1:1:1: F401 'os' imported but unused
133+
{expected_path_0}:cell_1:3:1: F401 'glob' imported but unused
134+
{expected_path_0}:cell_1:5:1: F401 'nbqa' imported but unused
135+
{expected_path_0}:cell_2:19:9: W291 trailing whitespace
136+
{expected_path_0}:cell_4:1:1: F401 'random.randint' imported but unused
137+
"""
138+
)
139+
assert err == ""
140+
assert sorted(out.splitlines()) == sorted(expected_out.splitlines())

0 commit comments

Comments
 (0)