如何抑制python警告模块中的堆栈级别

Dav*_*542 5 python python-3.x

如果我这样做,warnings.warn(...)我会得到这样的东西:

as.py:80: UserWarning: Value of 16777215 shortened to 8-bit value of 255
  warnings.warn(f'Value of {self.value} shortened to {size}-bit value of {value}')
Run Code Online (Sandbox Code Playgroud)

默认情况下它会打印stacklevel=1,我可以更改堆栈级别以打印父调用者等。我如何完全压制第二行?所以我只能这样做:

warnings.warn('Here is your warning')
Run Code Online (Sandbox Code Playgroud)

它打印:

file.py:80: Here is your warning
Run Code Online (Sandbox Code Playgroud)

或者这不是真的可能,如果您只想打印一个无堆栈警告来做log.warn(...)