[flake8-bugbear] Skip B028 if warnings.warn is called with *args or **kwargs (#14870)

This commit is contained in:
Harutaka Kawamura 2024-12-09 23:32:37 +09:00 committed by GitHub
parent 172143ae77
commit 9c3c59aca9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 13 deletions

View file

@ -11,6 +11,13 @@ warnings.warn("test", DeprecationWarning, source=None, stacklevel=2)
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",

View file

@ -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());

View file

@ -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 | )