-
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.
## Summary This PR updates `B015` and `B018` to ignore last top-level expressions in each cell of a Jupyter Notebook. Part of #8669 ## Test Plan Add test cases for both rules and update the snapshots.
- Loading branch information
1 parent
727e389
commit 5b726f7
Showing
11 changed files
with
454 additions
and
1 deletion.
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B015.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,149 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "33faf7ad-a3fd-4ac4-a0c3-52e507ed49df", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x = 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "481fb4bf-c1b9-47da-927f-3cfdfe4b49ec", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Simple case\n", | ||
"x == 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "2f0c65a5-0a0e-4080-afce-5a8ed0d706df", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Only skip the last expression\n", | ||
"x == 1 # B018\n", | ||
"x == 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "5a3fd75d-26d9-44f7-b013-1684aabfd0ae", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Nested expressions isn't relevant\n", | ||
"if True:\n", | ||
" x == 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "e00e1afa-b76c-4774-be2a-7223641579e4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Semicolons shouldn't affect the output\n", | ||
"x == 1;" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "05eab5b9-e2ba-4954-8ef3-b035a79573fe", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Semicolons with multiple expressions\n", | ||
"x == 1; x == 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "9cbbddc5-83fc-4fdb-81ab-53a3912ae898", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"True" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Comments, newlines and whitespace\n", | ||
"x == 1 # comment\n", | ||
"\n", | ||
"# another comment" | ||
] | ||
} | ||
], | ||
"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 | ||
} |
149 changes: 149 additions & 0 deletions
149
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B018.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,149 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "33faf7ad-a3fd-4ac4-a0c3-52e507ed49df", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x = 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "481fb4bf-c1b9-47da-927f-3cfdfe4b49ec", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1" | ||
] | ||
}, | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Simple case\n", | ||
"x" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "2f0c65a5-0a0e-4080-afce-5a8ed0d706df", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1" | ||
] | ||
}, | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Only skip the last expression\n", | ||
"x # B018\n", | ||
"x" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "5a3fd75d-26d9-44f7-b013-1684aabfd0ae", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Nested expressions isn't relevant\n", | ||
"if True:\n", | ||
" x" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "e00e1afa-b76c-4774-be2a-7223641579e4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Semicolons shouldn't affect the output\n", | ||
"x;" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "05eab5b9-e2ba-4954-8ef3-b035a79573fe", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Semicolons with multiple expressions\n", | ||
"x; x" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "9cbbddc5-83fc-4fdb-81ab-53a3912ae898", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Comments, newlines and whitespace\n", | ||
"x # comment\n", | ||
"\n", | ||
"# another comment" | ||
] | ||
} | ||
], | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,6 +163,7 @@ pub fn check_path( | |
path, | ||
package, | ||
source_type, | ||
cell_offsets, | ||
)); | ||
} | ||
if use_imports { | ||
|
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,28 @@ | ||
use ruff_notebook::CellOffsets; | ||
use ruff_python_semantic::SemanticModel; | ||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer}; | ||
use ruff_source_file::Locator; | ||
use ruff_text_size::{Ranged, TextRange}; | ||
|
||
/// Return `true` if the statement containing the current expression is the last | ||
/// top-level expression in the cell. This assumes that the source is a Jupyter | ||
/// Notebook. | ||
pub(super) fn at_last_top_level_expression_in_cell( | ||
semantic: &SemanticModel, | ||
locator: &Locator, | ||
cell_offsets: Option<&CellOffsets>, | ||
) -> bool { | ||
if !semantic.at_top_level() { | ||
return false; | ||
} | ||
let current_statement_end = semantic.current_statement().end(); | ||
cell_offsets | ||
.and_then(|cell_offsets| cell_offsets.containing_range(current_statement_end)) | ||
.is_some_and(|cell_range| { | ||
SimpleTokenizer::new( | ||
locator.contents(), | ||
TextRange::new(current_statement_end, cell_range.end()), | ||
) | ||
.all(|token| token.kind() == SimpleTokenKind::Semi || token.kind().is_trivia()) | ||
}) | ||
} |
Oops, something went wrong.