Skip to content

Commit

Permalink
B028: Allow stacklevel to be explicitly assigned as a positional argu…
Browse files Browse the repository at this point in the history
…ment (#374)
  • Loading branch information
FozzieHi authored Mar 23, 2023
1 parent fce31d4 commit d4c77fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -325,6 +325,10 @@ MIT
Change Log
----------

Unreleased
~~~~~~~~~~
* B028: Allow stacklevel to be explicitly assigned as a positional argument

23.3.23
~~~~~~~~~~

Expand Down
3 changes: 2 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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"
Expand Down
10 changes: 6 additions & 4 deletions tests/b028.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d4c77fe

Please sign in to comment.