python config_error ai_generated true

DeprecationWarning: datetime.datetime.utcnow() 已弃用并计划移除

DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal

ID: python/pytest-warnings-as-errors-failure

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

版本兼容性

版本状态引入弃用备注
3.12 active — — —
3.13 active — — —

根因分析

测试套件以 -W error 或 filterwarnings=error 运行,将应用或第三方代码的 DeprecationWarning 升级为测试失败。

English

The test suite runs with -W error or filterwarnings=error, promoting a DeprecationWarning from application or third-party code into a test failure.

generic

解决方案

  1. 95% 成功率
    from datetime import datetime, timezone
    dt = datetime.now(timezone.utc)
  2. 88% 成功率
    # pyproject.toml
    [tool.pytest.ini_options]
    filterwarnings = [
      "error",
      "ignore::DeprecationWarning:legacy_lib.*",
      "ignore:datetime.datetime.utcnow:DeprecationWarning",
    ]

无效尝试

常见但无效的做法:

  1. 85% 失败

    noqa is for linters; pytest's warning filter isn't affected by inline noqa comments.

  2. 70% 失败

    Doesn't fix the deprecated call; the warning still fires elsewhere in the codebase.