Warnings & Deprecations
The warnings module signals recoverable issues and API deprecations without raising exceptions. Configure filters to show, ignore, or escalate warnings - libraries use DeprecationWarning for scheduled removals.
Search across all documentation pages
The warnings module signals recoverable issues and API deprecations without raising exceptions. Configure filters to show, ignore, or escalate warnings - libraries use DeprecationWarning for scheduled removals.
import warnings
warnings.warn("old_api is deprecated; use new_api", DeprecationWarning, stacklevel=2)When to reach for this:
-W error turns warnings into test failuresUserWarning) from internal (DeprecationWarning)import warnings
warnings.simplefilter("default", DeprecationWarning)
def old_price(amount: float) -> float:
warnings.warn(
"old_price deprecated; use compute_price",
DeprecationWarning,
stacklevel=2,
)
return amount * 1.1
def compute_price(amount: float) -> float:
return amount * 1.1
warnings.filterwarnings("once", category=UserWarning, module=__name__)
def maybe_risky(flag: bool) -> None:
if flag:
warnings.warn("experimental path enabled", UserWarning, stacklevel=2)
old_price(10)
maybe_risky(True)
maybe_risky(True) # once filter suppresses duplicateWhat this demonstrates:
stacklevel=2 points warning at caller, not wrapperDeprecationWarning category for API sunsetfilterwarnings("once") avoids log spamwarnings.warn(message, category, stacklevel) emits WarningMessagewarnings.filterwarnings(action, category, ...) controls behaviorpytest.warns asserts warnings in tests@warnings.deprecated decorator (3.13+) for functions| Category | Audience |
|---|---|
DeprecationWarning | Developers (hidden from users by default) |
UserWarning | End users of API |
PendingDeprecationWarning | Early notice |
warnings.simplefilter for app entry or run tests with -W default::DeprecationWarning.-W error fails on any warning. Fix: treat deprecations as errors, allowlist third-party noise temporarily.| Alternative | Use When | Don't Use When |
|---|---|---|
| Raise exception | Hard breaking change now | Gradual migration needed |
| Logging only | Ops events | API contract deprecation |
Typing @deprecated | Static analysis hints | Runtime notice required |
pytest.deprecated_call() or with warnings.catch_warnings(record=True).
Yes for your code - PYTHONWARNINGS=error::DeprecationWarning catches stragglers before removal.
Python 3.13+ decorator marking callables deprecated with consistent messaging.
Document policy (e.g., two minor releases) and log usage metrics if possible.
logging.captureWarnings(True) routes warnings to logging subsystem.
filterwarnings("ignore", module="noisy_lib") - narrow scope, add comment with issue link.
FutureWarning for end-user visible behavior changes; DeprecationWarning for developer APIs.
Yes - warn when subclasses override removed hooks.
PEP 702 formalizes @deprecated metadata for type checkers and runtime in 3.13+.
Libraries warn; applications configure filters. Never simplefilter("ignore") globally in libraries.
Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 19, 2026