-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
E402
to work at cell level for notebooks (#8872)
## Summary This PR updates the `E402` rule to work at cell level for Jupyter notebooks. This is enabled only in preview to gather feedback. The implementation basically resets the import boundary flag on the semantic model when we encounter the first statement in a cell. Another potential solution is to introduce `E403` rule that is specifically for notebooks that works at cell level while `E402` will be disabled for notebooks. ## Test Plan Add a notebook with imports in multiple cells and verify that the rule works as expected. resolves: #8669
- Loading branch information
1 parent
4957d94
commit b28556d
Showing
7 changed files
with
204 additions
and
9 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
crates/ruff_linter/resources/test/fixtures/pycodestyle/E402.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,113 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "33faf7ad-a3fd-4ac4-a0c3-52e507ed49df", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import sys\n", | ||
"\n", | ||
"sys.path" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1331140f-2741-4661-9086-0764368710c9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a4113383-725d-4f04-80b8-a3080b2b8c4b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"\n", | ||
"os.path\n", | ||
"\n", | ||
"import pathlib" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a5d2ef63-ae60-4311-bae3-42e845afba3f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "79599475-a5ee-4f60-80d1-6efa77693da0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import a\n", | ||
"\n", | ||
"try:\n", | ||
" import b\n", | ||
"except ImportError:\n", | ||
" pass\n", | ||
"else:\n", | ||
" pass\n", | ||
"\n", | ||
"__some__magic = 1\n", | ||
"\n", | ||
"import c" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "863dcc35-5c8d-4d05-8b4a-91059e944112", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ok\n", | ||
"\n", | ||
"\n", | ||
"def foo() -> None:\n", | ||
" import e\n", | ||
"\n", | ||
"\n", | ||
"import no_ok" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6b2377d0-b814-4057-83ec-d443d8e19401", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python (ruff-playground)", | ||
"language": "python", | ||
"name": "ruff-playground" | ||
}, | ||
"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.11.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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
29 changes: 29 additions & 0 deletions
29
.../rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E402_E402.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,29 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs | ||
--- | ||
E402.ipynb:9:1: E402 Module level import not at top of cell | ||
| | ||
7 | os.path | ||
8 | | ||
9 | import pathlib | ||
| ^^^^^^^^^^^^^^ E402 | ||
10 | | ||
11 | import a | ||
| | ||
|
||
E402.ipynb:22:1: E402 Module level import not at top of cell | ||
| | ||
20 | __some__magic = 1 | ||
21 | | ||
22 | import c | ||
| ^^^^^^^^ E402 | ||
23 | import ok | ||
| | ||
|
||
E402.ipynb:30:1: E402 Module level import not at top of cell | ||
| | ||
30 | import no_ok | ||
| ^^^^^^^^^^^^ E402 | ||
| | ||
|
||
|
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