Skip to content

Commit

Permalink
[flake8-bugbear] Skip B028 if warnings.warn is called with `*ar…
Browse files Browse the repository at this point in the history
…gs` or `**kwargs` (#14870)
  • Loading branch information
harupy authored Dec 9, 2024
1 parent 172143a commit 9c3c59a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
warnings.warn("test", DeprecationWarning, stacklevel=1)
warnings.warn("test", DeprecationWarning, 1)
warnings.warn("test", category=DeprecationWarning, stacklevel=1)
args = ("test", DeprecationWarning, 1)
warnings.warn(*args)
kwargs = {"message": "test", "category": DeprecationWarning, "stacklevel": 1}
warnings.warn(**kwargs)
args = ("test", DeprecationWarning)
kwargs = {"stacklevel": 1}
warnings.warn(*args, **kwargs)

warnings.warn(
"test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ pub(crate) fn no_explicit_stacklevel(checker: &mut Checker, call: &ast::ExprCall
return;
}

if call.arguments.find_argument("stacklevel", 2).is_some() {
if call.arguments.find_argument("stacklevel", 2).is_some()
|| call
.arguments
.args
.iter()
.any(ruff_python_ast::Expr::is_starred_expr)
|| call
.arguments
.keywords
.iter()
.any(|keyword| keyword.arg.is_none())
{
return;
}
let mut diagnostic = Diagnostic::new(NoExplicitStacklevel, call.func.range());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ B028.py:9:1: B028 [*] No explicit `stacklevel` keyword argument found
12 12 | warnings.warn("test", DeprecationWarning, 1)
13 13 | warnings.warn("test", category=DeprecationWarning, stacklevel=1)

B028.py:15:1: B028 [*] No explicit `stacklevel` keyword argument found
B028.py:22:1: B028 [*] No explicit `stacklevel` keyword argument found
|
13 | warnings.warn("test", category=DeprecationWarning, stacklevel=1)
14 |
15 | warnings.warn(
20 | warnings.warn(*args, **kwargs)
21 |
22 | warnings.warn(
| ^^^^^^^^^^^^^ B028
16 | "test",
17 | DeprecationWarning,
23 | "test",
24 | DeprecationWarning,
|
= help: Set `stacklevel=2`

Unsafe fix
16 16 | "test",
17 17 | DeprecationWarning,
18 18 | # some comments here
19 |- source = None # no trailing comma
19 |+ source = None, stacklevel=2 # no trailing comma
20 20 | )
23 23 | "test",
24 24 | DeprecationWarning,
25 25 | # some comments here
26 |- source = None # no trailing comma
26 |+ source = None, stacklevel=2 # no trailing comma
27 27 | )

0 comments on commit 9c3c59a

Please sign in to comment.