【问题标题】:How does -W interact with DeprecationWarning and PendingDeprecationWarning?-W 如何与 DeprecationWarning 和 PendingDeprecationWarning 交互?
【发布时间】:2020-08-07 22:19:46
【问题描述】:

如何使用DeprecationWarningPendingDeprecationWarning 通知开发人员/测试人员(但不是最终用户)即将弃用以及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


    【解决方案1】:

    您将 -Wa-Wi 作为参数传递给脚本。您需要将它们放在脚本名称之前,以将它们视为解释器本身的选项。

    【讨论】:

    • 哇哦!失败!好可怜哈哈。我现在要订购 3 个橡皮鸭子放在我的桌子周围。哈哈。谢谢!
    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2017-04-25
    • 2011-02-15
    • 2015-09-22
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多