Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ limitations make it difficult.
**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.
The check is skipped when skip_file_prefixes is used.
The check is skipped when ``skip_file_prefixes`` is used, except for an explicitly empty tuple,
which does not affect ``stacklevel``.

.. _B029:

Expand Down Expand Up @@ -500,6 +501,8 @@ Change Log
UNRELEASED
~~~~~~~~~~

* B028: report ``warnings.warn`` calls that pass an explicitly empty
``skip_file_prefixes`` tuple (#510)
* B019: also flag `async_lru.alru_cache` and check cache decorators on `async def` methods (#488)
* B023: don't flag a function whose every reference is a direct call inside the loop body:
such a function cannot outlive the iteration it was defined in (#468, #380)
Expand Down
6 changes: 5 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,11 @@ def check_for_b028(self, node: ast.Call) -> None:
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 not any(kw.arg == "skip_file_prefixes" for kw in node.keywords)
and all(
kw.arg != "skip_file_prefixes"
or (isinstance(kw.value, ast.Tuple) and not kw.value.elts)
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)
Expand Down
5 changes: 4 additions & 1 deletion tests/eval_files/b028.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

"""
Should emit:
B028 - on lines 8 and 9
B028 - on lines 8, 9, and 20
"""

warnings.warn("test", DeprecationWarning) # B028: 0
Expand All @@ -17,3 +17,6 @@
warnings.warn(**kwargs)
warnings.warn(*args, **kwargs)
warnings.warn("test", DeprecationWarning, skip_file_prefixes=["foo"])
warnings.warn("test", DeprecationWarning, skip_file_prefixes=()) # B028: 0
skip_file_prefixes = ()
warnings.warn("test", DeprecationWarning, skip_file_prefixes=skip_file_prefixes)