Skip to content
Latchkey

pytest warning turned into an error by filterwarnings in CI

The config sets filterwarnings = error, so pytest promotes warnings to exceptions. A library that emits a DeprecationWarning then fails the test, even though the assertion itself passed.

What this error means

A test fails with a warning class shown as the exception, for example "DeprecationWarning: ... " raised as an error, traced to library code rather than your assertion. It often appears after a dependency upgrade.

pytest
E   DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal
E   in a future version. Use timezone-aware objects instead.

tests/test_time.py:8: DeprecationWarning

Common causes

filterwarnings = error promotes all warnings

A strict warnings filter turns any emitted warning into a hard failure, so a newly deprecated call breaks the suite.

A dependency upgrade introduced a new warning

An updated library started emitting a DeprecationWarning that was harmless before the strict filter was added.

How to fix it

Fix the deprecated call

  1. Read which warning and which line the error points at.
  2. Update the code to the recommended non-deprecated API.
  3. Re-run so no warning is emitted.
app/time.py
# before: datetime.utcnow()
from datetime import datetime, timezone
datetime.now(timezone.utc)

Scope an ignore for third-party warnings

If the warning comes from a dependency you cannot change yet, ignore that specific warning rather than disabling the strict filter entirely.

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

How to prevent it

  • Address deprecations promptly so the strict filter stays clean.
  • Ignore only specific third-party warnings, never blanket-disable.
  • Run the suite after dependency upgrades to catch new warnings early.

Related guides

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