【发布时间】:2020-08-07 22:19:46
【问题描述】:
如何使用DeprecationWarning 和PendingDeprecationWarning 通知开发人员/测试人员(但不是最终用户)即将弃用以及python foo.py -W 控件?
下面的 sn-p 是对我所期望的不起作用的独立重现,但我也不知道如何使 -W 工作。
test_deprecation.py
from warnings import warn
def warningfunction():
warn("this is deprecated", DeprecationWarning, 2)
def pendingfunction():
warn("pending", PendingDeprecationWarning, 2)
def test_warning():
warningfunction()
def test_pending():
pendingfunction()
if __name__ == '__main__':
warningfunction()
pendingfunction()
以pytest test_deprecation.py -vv 运行,两个警告都按预期出现。这至少是好的!
以python test_deprecation.py 运行,只收到第一个警告(因为它在 main 中)。这也是我所期望的!
以python test_deprecation.py -Wa 运行,仍然只收到第一个警告。这是我没想到的部分。 -Wa 不是应该打开所有警告吗?
以python test_deprecation.py -Wi 运行(忽略警告),仍然只收到第一个警告。也不预期。 -Wi 不应该忽略所有警告吗?
-Wdefault # Warn once per call location
-Werror # Convert to exceptions
-Walways # Warn every time
-Wmodule # Warn once per calling module
-Wonce # Warn once per Python process
-Wignore # Never warn
动作名称可以根据需要缩写(例如 -Wi、-Wd、-Wa、-We),解释器会将它们解析为适当的动作名称。
来自https://docs.python.org/3/using/cmdline.html#cmdoption-w
(python3.8 顺便说一句)
echo $PYTHONWARNINGS 为空
【问题讨论】:
标签: python python-3.x warnings deprecated deprecation-warning