python config_error ai_generated true

DeprecationWarning: datetime.datetime.utcnow() 已弃用并计划在未来版本中移除。 E DeprecationWarning: datetime.datetime.utcnow() 已弃用

DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. E DeprecationWarning: datetime.datetime.utcnow() is deprecated

ID: python/pytest-deprecationwarning-as-error

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-09-11首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

pytest 配置了 filterwarnings = error,将 DeprecationWarning 变为测试失败;代码或依赖仍调用 utcnow()。

English

pytest is configured with filterwarnings = error, turning DeprecationWarning into a test failure; the code or a dependency still calls utcnow().

generic

解决方案

  1. 95% 成功率
    from datetime import datetime, timezone
    now = datetime.now(timezone.utc)
  2. 80% 成功率
    # pytest.ini
    # filterwarnings =
    #   error
    #   ignore:datetime.datetime.utcnow:DeprecationWarning:legacy_lib

无效尝试

常见但无效的做法:

  1. 60% 失败

    Hides all warnings and defeats the purpose; future breakages go unnoticed.

  2. 55% 失败

    Ignores all deprecations including ones from your own code; not targeted.