python config_error ai_generated true

DeprecationWarning: datetime.datetime.utcnow() 已弃用 FAILED tests/test_time.py::test_now - DeprecationWarning: ...

DeprecationWarning: datetime.datetime.utcnow() is deprecated FAILED tests/test_time.py::test_now - DeprecationWarning: ...

ID: python/pytest-warnings-as-errors-failing-ci

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

版本兼容性

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

根因分析

CI 使用 `-W error`(或配置中的 `filterwarnings = error`)运行 pytest,因此测试执行期间抛出的任何 DeprecationWarning 都会变成失败。这通常在 Python 小版本升级后出现。

English

CI runs pytest with `-W error` (or `filterwarnings = error` in config), so any DeprecationWarning raised during test execution becomes a failure. This often appears after a Python minor upgrade.

generic

解决方案

  1. 90% 成功率
    Ignore specific known-safe warnings via pytest config:
    
    # pyproject.toml
    [tool.pytest.ini_options]
    filterwarnings = [
      "error",
      "ignore:datetime.datetime.utcnow\(\) is deprecated:DeprecationWarning",
    ]
  2. 98% 成功率
    Fix the deprecated call site:
    
    from datetime import datetime, timezone
    datetime.now(timezone.utc)  # replaces utcnow()

无效尝试

常见但无效的做法:

  1. 60% 失败

    Hides all warnings, including ones the team intentionally turned into errors to catch regressions.

  2. 70% 失败

    Only suppresses in that scope; other tests using the same deprecated API still fail.