-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-bugbear
] Add no-explicit-stacklevel
(B028
)
#3550
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
crates/ruff/resources/test/fixtures/flake8_bugbear/B028.py
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,11 @@ | ||
import warnings | ||
|
||
""" | ||
Should emit: | ||
B028 - on lines 8 and 9 | ||
""" | ||
|
||
warnings.warn(DeprecationWarning("test")) | ||
warnings.warn(DeprecationWarning("test"), source=None) | ||
warnings.warn(DeprecationWarning("test"), source=None, stacklevel=2) | ||
warnings.warn(DeprecationWarning("test"), stacklevel=1) |
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
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
68 changes: 68 additions & 0 deletions
68
crates/ruff/src/rules/flake8_bugbear/rules/no_explicit_stacklevel.rs
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,68 @@ | ||
use rustpython_parser::ast::{Expr, Keyword}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::SimpleCallArgs; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `warnings.warn` calls without an explicit `stacklevel` keyword | ||
/// argument. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `warnings.warn` method uses a `stacklevel` of 1 by default, which | ||
/// limits the rendered stack trace to that of the line on which the | ||
/// `warn` method is called. | ||
/// | ||
/// It's recommended to use a `stacklevel` of 2 or higher, give the caller | ||
/// more context about the warning. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// warnings.warn("This is a warning") | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// warnings.warn("This is a warning", stacklevel=2) | ||
/// ``` | ||
#[violation] | ||
pub struct NoExplicitStacklevel; | ||
|
||
impl Violation for NoExplicitStacklevel { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("No explicit `stacklevel` keyword argument found") | ||
} | ||
} | ||
|
||
/// B028 | ||
pub fn no_explicit_stacklevel( | ||
checker: &mut Checker, | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
) { | ||
if !checker | ||
.ctx | ||
.resolve_call_path(func) | ||
.map_or(false, |call_path| { | ||
call_path.as_slice() == ["warnings", "warn"] | ||
}) | ||
{ | ||
return; | ||
} | ||
|
||
if SimpleCallArgs::new(args, keywords) | ||
.keyword_argument("stacklevel") | ||
.is_some() | ||
{ | ||
return; | ||
} | ||
|
||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(NoExplicitStacklevel, Range::from(func))); | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B028_B028.py.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,31 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_bugbear/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: NoExplicitStacklevel | ||
body: "No explicit `stacklevel` keyword argument found" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 8 | ||
column: 0 | ||
end_location: | ||
row: 8 | ||
column: 13 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: NoExplicitStacklevel | ||
body: "No explicit `stacklevel` keyword argument found" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 9 | ||
column: 0 | ||
end_location: | ||
row: 9 | ||
column: 13 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I truncated this message a bit and instead moved the additional context into docs.