python config_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.12 active — — —
3.13 active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 85% fail

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

  2. 70% fail

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