Sub topic 7
Turn deprecation warnings into failures
This chapter assumes you can run Python scripts, use pip, and run tests with pytest. It also assumes you have read the earlier chapters on pinning versions and checking that a symbol exists.
Why deprecations matter for generated code
An assistant often writes code that was correct for an older release. Many such calls still run. The library keeps them for a while and prints a DeprecationWarning. Python hides that warning in most situations, so the code looks fine until a later upgrade removes the call.
Two kinds of outdated usage are common:
- Deprecated but present. The call works now and warns you. Examples are
datetime.utcnow()on Python 3.12 and later. - Removed. The call fails immediately.
DataFrame.appendwas removed in pandas 2.0.
Warnings are your early signal for the first kind. They give you time to fix the code before it becomes the second kind.
Why you rarely see the warning
By default, Python ignores DeprecationWarning unless it is triggered directly by code in __main__. A deprecated call inside your own module or inside a library is silent in a normal run. Pytest changes this: it collects warnings and shows them in a summary at the end. That summary is easy to skip when the tests pass.
Fail on deprecated calls in a script
The warnings module lets you change how a warning is handled. Setting the action to "error" raises the warning as an exception.
import warnings
from datetime import datetime
warnings.simplefilter("error", DeprecationWarning)
# On Python 3.12 and later this line now raises DeprecationWarning.
now = datetime.utcnow()On Python 3.12 or later, this script stops at the last line with a traceback that names utcnow. The replacement is a timezone-aware call:
from datetime import datetime, timezone
now = datetime.now(timezone.utc)Put the filter at the entry point of the program, before the code you want to check runs. A filter set after the deprecated call has no effect on it.
Fail on deprecated calls in a test run
You do not need to edit source files. The -W option sets a warning filter for the whole run:
python -W error::DeprecationWarning -m pytestAny test that executes a deprecated call now fails with the warning as its error. This works well on a pull request that adds generated code, because the failing test points at the exact line.
For a permanent setting, add a filter to your pytest configuration. This example uses pyproject.toml:
[tool.pytest.ini_options]
filterwarnings = [
"error::DeprecationWarning",
"error::PendingDeprecationWarning",
]PendingDeprecationWarning is a weaker signal. Libraries use it for features they plan to deprecate later.
Common problems
Turning on strict filters in an existing project can fail for reasons that are not your fault. Handle these cases deliberately:
- Warnings from third-party packages. A dependency may use a deprecated call internally. You cannot fix it. Ignore that source narrowly, for example
"ignore::DeprecationWarning:somepackage.*", and keep the globalerrorrule for everything else. - Warnings that fire at import time. Some libraries warn when you import them. The failure then appears during test collection, so read the traceback to find which import caused it.
- Code that never runs. A filter only catches calls that execute. A deprecated call in a branch no test reaches stays hidden. The next chapter covers writing tests that reach those paths.
- Wrong warning category. Some libraries use
FutureWarningfor warnings meant for end users, and pandas has used it for behavior changes. If a library you rely on does this, adderror::FutureWarningas well.
Reading the message
A deprecation message usually names the replacement. For example, the utcnow warning suggests datetime.now(datetime.UTC). Check the replacement against the versioned documentation, as described in the earlier chapter, before you apply it. Do not ask the assistant to guess the fix without showing it the exact warning text.
Exercise
A teammate pastes assistant-generated code that calls datetime.utcnow(). The code runs on Python 3.12 and the tests pass, but you suspect it is outdated. Which command makes the tests fail on this call, and what should you replace the call with?
Show answer
Run python -W error::DeprecationWarning -m pytest. The test that executes the call now fails with the DeprecationWarning as the error. Replace datetime.utcnow() with datetime.now(timezone.utc), which needs from datetime import datetime, timezone and returns a timezone-aware value.
Exercise
You enable error::DeprecationWarning in pyproject.toml and the suite fails during collection because a third-party package warns when imported. How do you keep the strict rule for your own code?
Show answer
Add a narrower ignore filter for that package, such as "ignore::DeprecationWarning:somepackage.*", after the general error rule or as a more specific entry. Keep error::DeprecationWarning for everything else, so deprecations in your own and generated code still fail.