-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
pycodestyle
] Handle each cell separately for `too-many-newlines-at…
…-end-of-file` (`W391`) (#15308) Jupyter notebooks are converted into source files by joining with newlines, which confuses the check [too-many-newlines-at-end-of-file (W391)](https://docs.astral.sh/ruff/rules/too-many-newlines-at-end-of-file/#too-many-newlines-at-end-of-file-w391). This PR introduces logic to apply the check cell-wise (and, in particular, correctly handles empty cells.) Closes #13763
- Loading branch information
Showing
5 changed files
with
261 additions
and
31 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
crates/ruff_linter/resources/test/fixtures/pycodestyle/W391.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"True" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# just a comment in this cell" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# a comment and some newlines\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"1 + 1\n", | ||
"# a comment\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"1+1\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...codestyle/snapshots/ruff_linter__rules__pycodestyle__tests__preview__W391_W391.ipynb.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
W391.ipynb:5:1: W391 [*] Too many newlines at end of cell | ||
| | ||
3 | # just a comment in this cell | ||
4 | # a comment and some newlines | ||
5 | / | ||
6 | | | ||
7 | | | ||
8 | | | ||
| |_^ W391 | ||
9 | 1 + 1 | ||
10 | # a comment | ||
| | ||
= help: Remove trailing newlines | ||
|
||
ℹ Safe fix | ||
3 3 | # just a comment in this cell | ||
4 4 | # a comment and some newlines | ||
5 5 | | ||
6 |- | ||
7 |- | ||
8 |- | ||
9 6 | 1 + 1 | ||
10 7 | # a comment | ||
11 8 | | ||
|
||
W391.ipynb:11:1: W391 [*] Too many newlines at end of cell | ||
| | ||
9 | 1 + 1 | ||
10 | # a comment | ||
11 | / | ||
12 | | | ||
13 | | | ||
14 | | | ||
15 | | | ||
| |_^ W391 | ||
16 | 1+1 | ||
| | ||
= help: Remove trailing newlines | ||
|
||
ℹ Safe fix | ||
9 9 | 1 + 1 | ||
10 10 | # a comment | ||
11 11 | | ||
12 |- | ||
13 |- | ||
14 |- | ||
15 |- | ||
16 12 | 1+1 | ||
17 13 | | ||
18 14 | | ||
|
||
W391.ipynb:17:1: W391 [*] Too many newlines at end of cell | ||
| | ||
16 | 1+1 | ||
17 | / | ||
18 | | | ||
19 | | | ||
20 | | | ||
21 | | | ||
| |_^ W391 | ||
| | ||
= help: Remove trailing newlines | ||
|
||
ℹ Safe fix | ||
15 15 | | ||
16 16 | 1+1 | ||
17 17 | | ||
18 |- | ||
19 |- | ||
20 |- | ||
21 |- |