Skip to content

Commit 215185a

Browse files
LaBatata101Glyphack
authored andcommitted
[flake8-bugbear] Ignore B028 if skip_file_prefixes is present (astral-sh#18047)
## Summary Fixes astral-sh#18011
1 parent fadf6e2 commit 215185a

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B028.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@
2525
# some comments here
2626
source = None # no trailing comma
2727
)
28+
29+
# https://github.com/astral-sh/ruff/issues/18011
30+
warnings.warn("test", skip_file_prefixes=(os.path.dirname(__file__),))
31+
# trigger diagnostic if `skip_file_prefixes` is present and set to the default value
32+
warnings.warn("test", skip_file_prefixes=())
33+
34+
_my_prefixes = ("this","that")
35+
warnings.warn("test", skip_file_prefixes = _my_prefixes)

crates/ruff_linter/src/rules/flake8_bugbear/rules/no_explicit_stacklevel.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Fix};
22
use ruff_macros::{derive_message_formats, ViolationMetadata};
3-
use ruff_python_ast::{self as ast};
3+
use ruff_python_ast::{self as ast, Expr};
44
use ruff_text_size::Ranged;
55

66
use crate::{checkers::ast::Checker, fix::edits::add_argument};
@@ -60,10 +60,18 @@ pub(crate) fn no_explicit_stacklevel(checker: &Checker, call: &ast::ExprCall) {
6060
return;
6161
}
6262

63+
// When prefixes are supplied, stacklevel is implicitly overridden to be `max(2, stacklevel)`.
64+
//
65+
// Signature as of Python 3.13 (https://docs.python.org/3/library/warnings.html#warnings.warn)
66+
// ```text
67+
// 0 1 2 3 4
68+
// warnings.warn(message, category=None, stacklevel=1, source=None, *, skip_file_prefixes=())
69+
// ```
6370
if call
6471
.arguments
6572
.find_argument_value("stacklevel", 2)
6673
.is_some()
74+
|| is_skip_file_prefixes_param_set(&call.arguments)
6775
|| call
6876
.arguments
6977
.args
@@ -90,3 +98,14 @@ pub(crate) fn no_explicit_stacklevel(checker: &Checker, call: &ast::ExprCall) {
9098

9199
checker.report_diagnostic(diagnostic);
92100
}
101+
102+
/// Returns `true` if `skip_file_prefixes` is set to its non-default value.
103+
/// The default value of `skip_file_prefixes` is an empty tuple.
104+
fn is_skip_file_prefixes_param_set(arguments: &ast::Arguments) -> bool {
105+
arguments
106+
.find_keyword("skip_file_prefixes")
107+
.is_some_and(|keyword| match &keyword.value {
108+
Expr::Tuple(tuple) => !tuple.elts.is_empty(),
109+
_ => true,
110+
})
111+
}

crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B028_B028.py.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,26 @@ B028.py:22:1: B028 [*] No explicit `stacklevel` keyword argument found
6161
26 |- source = None # no trailing comma
6262
26 |+ source = None, stacklevel=2 # no trailing comma
6363
27 27 | )
64+
28 28 |
65+
29 29 | # https://github.com/astral-sh/ruff/issues/18011
66+
67+
B028.py:32:1: B028 [*] No explicit `stacklevel` keyword argument found
68+
|
69+
30 | warnings.warn("test", skip_file_prefixes=(os.path.dirname(__file__),))
70+
31 | # trigger diagnostic if `skip_file_prefixes` is present and set to the default value
71+
32 | warnings.warn("test", skip_file_prefixes=())
72+
| ^^^^^^^^^^^^^ B028
73+
33 |
74+
34 | _my_prefixes = ("this","that")
75+
|
76+
= help: Set `stacklevel=2`
77+
78+
Unsafe fix
79+
29 29 | # https://github.com/astral-sh/ruff/issues/18011
80+
30 30 | warnings.warn("test", skip_file_prefixes=(os.path.dirname(__file__),))
81+
31 31 | # trigger diagnostic if `skip_file_prefixes` is present and set to the default value
82+
32 |-warnings.warn("test", skip_file_prefixes=())
83+
32 |+warnings.warn("test", skip_file_prefixes=(), stacklevel=2)
84+
33 33 |
85+
34 34 | _my_prefixes = ("this","that")
86+
35 35 | warnings.warn("test", skip_file_prefixes = _my_prefixes)

0 commit comments

Comments
 (0)