Skip to content
Latchkey

pytest "DeprecationWarning" Failing CI via filterwarnings=error

Your config promotes warnings to errors (filterwarnings = error or -W error). A DeprecationWarning from a dependency or your own code then fails the test that triggers it - even though nothing functionally broke.

What this error means

A test fails with the warning shown as the error and a traceback ending in DeprecationWarning: .... The same test passes when warnings are not errors. It often appears after a dependency upgrade introduces a new deprecation.

pytest output
tests/test_time.py::test_now
E   DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled
    for removal in a future version. Use timezone-aware objects ...
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

Common causes

filterwarnings=error promotes warnings to failures

A filterwarnings = error in config (or -W error) makes any warning raise. A new DeprecationWarning then fails the run, which is the intended early-warning behavior.

A dependency deprecation surfaced after an upgrade

Upgrading a library (or Python) introduces a deprecation in code paths your tests exercise, tripping the error filter.

How to fix it

Fix the deprecated usage (preferred)

Migrate off the deprecated API - that is the point of warnings-as-errors. Example: use timezone-aware datetimes.

Python
from datetime import datetime, timezone
now = datetime.now(timezone.utc)   # not datetime.utcnow()

Scope an ignore for third-party noise

If the deprecation is in a dependency you cannot fix yet, add a narrow ignore rather than disabling the whole error filter.

pyproject.toml
[tool.pytest.ini_options]
filterwarnings = [
  "error",
  "ignore:datetime.datetime.utcnow:DeprecationWarning:somelib",
]

How to prevent it

  • Keep filterwarnings = error to catch deprecations early, and fix them promptly.
  • Scope ignores to the specific message and module, not globally.
  • Run the suite against new dependency/Python versions before upgrading.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →