python config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-09-11First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active — — —
8.x active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 60% fail

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

  2. 55% fail

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