diff --git a/README.rst b/README.rst index 1db92bd..c819cca 100644 --- a/README.rst +++ b/README.rst @@ -173,7 +173,7 @@ limitations make it difficult. **B027**: Empty method in abstract base class, but has no abstract decorator. Consider adding @abstractmethod. -**B028**: No explicit stacklevel keyword argument found. The warn method from the warnings module uses a +**B028**: No explicit stacklevel argument found. The warn method from the warnings module uses a stacklevel of 1 by default. This will only show a stack trace for the line on which the warn method is called. It is therefore recommended to use a stacklevel of 2 or greater to provide more information to the user. @@ -325,6 +325,10 @@ MIT Change Log ---------- +Unreleased +~~~~~~~~~~ +* B028: Allow stacklevel to be explicitly assigned as a positional argument + 23.3.23 ~~~~~~~~~~ diff --git a/bugbear.py b/bugbear.py index ea3dbd3..ebc8d67 100644 --- a/bugbear.py +++ b/bugbear.py @@ -1290,6 +1290,7 @@ def check_for_b028(self, node): and isinstance(node.func.value, ast.Name) and node.func.value.id == "warnings" and not any(kw.arg == "stacklevel" for kw in node.keywords) + and len(node.args) < 3 ): self.errors.append(B028(node.lineno, node.col_offset)) @@ -1674,7 +1675,7 @@ def visit_Lambda(self, node): ) B028 = Error( message=( - "B028 No explicit stacklevel keyword argument found. The warn method from the" + "B028 No explicit stacklevel argument found. The warn method from the" " warnings module uses a stacklevel of 1 by default. This will only show a" " stack trace for the line on which the warn method is called." " It is therefore recommended to use a stacklevel of 2 or" diff --git a/tests/b028.py b/tests/b028.py index a2915f2..2c56306 100644 --- a/tests/b028.py +++ b/tests/b028.py @@ -5,7 +5,9 @@ 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) +warnings.warn("test", DeprecationWarning) +warnings.warn("test", DeprecationWarning, source=None) +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)