Skip to content

no-explicit-stacklevel (B028)

Derived from the flake8-bugbear linter.

What it does

Checks for warnings.warn calls without an explicit stacklevel keyword argument.

Why is this bad?

The warnings.warn method uses a stacklevel of 1 by default, which limits the rendered stack trace to that of the line on which the warn method is called.

It's recommended to use a stacklevel of 2 or higher, give the caller more context about the warning.

Example

warnings.warn("This is a warning")

Use instead:

warnings.warn("This is a warning", stacklevel=2)

References