python config_error ai_generated true

Failed: [pytest] filterwarnings error in '...': DeprecationWarning: datetime.datetime.utcnow() is deprecated

ID: python/pytest-warning-as-error-failed

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

Version Compatibility

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

Root Cause

The project configures filterwarnings = error, turning warnings into failures. A third-party library or the code under test emits a DeprecationWarning that is not in the ignore list.

generic

中文

项目配置了 filterwarnings = error,将警告转为失败。第三方库或被测代码发出未在忽略列表中的 DeprecationWarning。

Workarounds

  1. 90% success
    [tool.pytest.ini_options]
    filterwarnings = [
      "error",
      "ignore:datetime.datetime.utcnow:DeprecationWarning",
      "ignore:.*is deprecated:DeprecationWarning:thirdparty.module",
    ]
  2. 95% success
    # before:
    from datetime import datetime
    datetime.utcnow()
    
    # after:
    from datetime import datetime, timezone
    datetime.now(timezone.utc)

Dead Ends

Common approaches that don't work:

  1. 40% fail

    Reintroduces noisy warnings and hides genuine deprecations you wanted to catch.

  2. 70% fail

    Not applied in CI where the config is used; local passes but CI fails.