Skip to content

Commit

Permalink
When raising warning, always use stacklevel=2
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
  • Loading branch information
giampaolo committed Mar 27, 2023
1 parent 64b4318 commit f221655
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def __init__(self, seconds, pid=None, name=None):
if isinstance(__builtins__, dict): # cpython
exec_ = __builtins__["exec"]
else: # pypy
exec_ = getattr(__builtins__, "exec")
exec_ = getattr(__builtins__, "exec") # noqa

exec_("""def raise_from(value, from_value):
try:
Expand Down
6 changes: 3 additions & 3 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def virtual_memory():
msg = "%s memory stats couldn't be determined and %s set to 0" % (
", ".join(missing_fields),
"was" if len(missing_fields) == 1 else "were")
warnings.warn(msg, RuntimeWarning)
warnings.warn(msg, RuntimeWarning, stacklevel=2)

return svmem(total, avail, percent, used, free,
active, inactive, buffers, cached, shared, slab)
Expand Down Expand Up @@ -544,7 +544,7 @@ def swap_memory():
# see https://github.com/giampaolo/psutil/issues/722
msg = "'sin' and 'sout' swap memory stats couldn't " \
"be determined and were set to 0 (%s)" % str(err)
warnings.warn(msg, RuntimeWarning)
warnings.warn(msg, RuntimeWarning, stacklevel=2)
sin = sout = 0
else:
with f:
Expand All @@ -564,7 +564,7 @@ def swap_memory():
# https://github.com/giampaolo/psutil/issues/313
msg = "'sin' and 'sout' swap memory stats couldn't " \
"be determined and were set to 0"
warnings.warn(msg, RuntimeWarning)
warnings.warn(msg, RuntimeWarning, stacklevel=2)
sin = sout = 0
return _common.sswap(total, used, free, percent, sin, sout)

Expand Down
4 changes: 2 additions & 2 deletions psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def attempt(exe):
# Let's use the base python in this case.
base = getattr(sys, "_base_executable", None)
if WINDOWS and sys.version_info >= (3, 7) and base is not None:
# We need to set __PYVENV_LAUNCHER__ to sys.executable for the
# We need to set __PYVENV_LAUNCHER__ to sys.executable for the
# base python executable to know about the environment.
env["__PYVENV_LAUNCHER__"] = sys.executable
return base, env
Expand Down Expand Up @@ -1737,7 +1737,7 @@ def import_module_by_path(path):

def warn(msg):
"""Raise a warning msg."""
warnings.warn(msg, UserWarning)
warnings.warn(msg, UserWarning, stacklevel=2)


def is_namedtuple(x):
Expand Down

0 comments on commit f221655

Please sign in to comment.