From 4fed293390eff2db0e3ab0658d69828ac82dfb3e Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Tue, 10 Dec 2024 00:30:22 +0900 Subject: [PATCH] Skip `B028` if `warnings.warn` is called with `*args` or `**kwargs` (#501) * Avoid raising B028 if warnings.warn contains *args or **kwargs * Update tests/b028.py Co-authored-by: Cooper Lees --------- Co-authored-by: Cooper Lees --- bugbear.py | 2 ++ tests/b028.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/bugbear.py b/bugbear.py index 04cd1ae..53abddc 100644 --- a/bugbear.py +++ b/bugbear.py @@ -1683,6 +1683,8 @@ def check_for_b028(self, node) -> None: and node.func.value.id == "warnings" and not any(kw.arg == "stacklevel" for kw in node.keywords) and len(node.args) < 3 + and not any(isinstance(a, ast.Starred) for a in node.args) + and not any(kw.arg is None for kw in node.keywords) ): self.errors.append(B028(node.lineno, node.col_offset)) diff --git a/tests/b028.py b/tests/b028.py index 2c56306..62e761b 100644 --- a/tests/b028.py +++ b/tests/b028.py @@ -11,3 +11,8 @@ 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) +warnings.warn(*args, **kwargs)