DeprecationWarning
A notice that something will go away. The default filters hide it unless the code that triggered it lives in __main__ — so library deprecations stay invisible until you run with -W default, -X dev, or a test runner.
Demo
import warnings with warnings.catch_warnings(record=True) as caught: warnings.warn_explicit('f() is deprecated', DeprecationWarning, 'lib.py', 1, module='__main__') len(caught)
Only the exact module name __main__ gets a 1: the first default filter is default::DeprecationWarning:__main__, and its module field must equal the module name exactly. Any library module — even __main__.cli — falls through to ignore::DeprecationWarning and records nothing. In Handle mode, error turns the call into an exception (0 recorded), always lets it return 1 and records the warning.
Constructor
| Name | Type | Required | Description |
|---|---|---|---|
| *args | object | no | The message; warnings.warn(text, DeprecationWarning) builds it for you. |
Attributes
| Attribute | Type | Meaning |
|---|---|---|
| args | tuple | The constructor arguments; args[0] is the message text. |
| __deprecated__ | str | Not on the warning — set by @warnings.deprecated(msg) (3.13) on the decorated function or class, holding msg. |
Common patterns
from warnings import deprecated @deprecated('Use load_config() instead') def read_config(path): return load_config(path)
import warnings def read_config(path): warnings.warn('read_config() is deprecated; use load_config()', DeprecationWarning, stacklevel=2) return load_config(path)
# pytest.ini # [pytest] # filterwarnings = # error::DeprecationWarning import warnings warnings.simplefilter('error', DeprecationWarning)
Examples
Pitfalls
import warnings def old(x): warnings.warn('old() is deprecated', DeprecationWarning) return x with warnings.catch_warnings(record=True) as caught: warnings.simplefilter('always') old(1) [w.lineno for w in caught]
import warnings def old(x): warnings.warn('old() is deprecated', DeprecationWarning, stacklevel=2) return x with warnings.catch_warnings(record=True) as caught: warnings.simplefilter('always') old(1) [w.lineno for w in caught]
import warnings with warnings.catch_warnings(record=True) as caught: warnings.warn_explicit('option x is going away', DeprecationWarning, 'lib.py', 1, module='mylib') len(caught)
import warnings with warnings.catch_warnings(record=True) as caught: warnings.warn_explicit('option x is going away', FutureWarning, 'lib.py', 1, module='mylib') len(caught)
When to use
- DeprecationWarning: an API other developers call is going away
- PendingDeprecationWarning: it will be deprecated later (rarely used; hidden by default even in __main__)
- FutureWarning: users of an application must see it — the default filters do not hide it
- Messages for end users → FutureWarning, or plain output/logging
- The feature is already gone → raise a real exception (AttributeError, TypeError …)
Notes
FAQ
Release builds ignore DeprecationWarning unless it is attributed to code in __main__ (the script you ran). Deprecations triggered inside libraries — including your own imported modules — are hidden. Show them with python -W default::DeprecationWarning, python -X dev, PYTHONWARNINGS=default, or warnings.simplefilter('default', DeprecationWarning) at startup. Test runners such as pytest show them in their summary.