Full answer
python
import warnings, datetime, sys, importlib.util
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
datetime.datetime.utcnow()
print(sys.version_info[:2], [w.category.__name__ for w in caught])
print(caught[0].message.args[0].split(".")[0])
print({m: importlib.util.find_spec(m) is not None for m in ["cgi", "imghdr", "telnetlib", "tomllib"]})
print(datetime.datetime.now(datetime.UTC).tzinfo)Output:
text
(3, 13) ['DeprecationWarning']
datetime
{'cgi': False, 'imghdr': False, 'telnetlib': False, 'tomllib': True}
UTCOn 3.13, datetime.utcnow() emits a DeprecationWarning because it returns a naive datetime; the replacement is datetime.now(datetime.UTC), which is timezone-aware. The module check shows that cgi, imghdr and telnetlib no longer exist, while tomllib (added in 3.11) does. A migration plan built on this:
- Run tests on 3.8 with
-W error::DeprecationWarningto surface problems early. - Inventory dependencies; upgrade or replace those without 3.13 wheels.
- Add 3.13 to the CI matrix; fix removed modules, changed defaults and C-extension builds.
- Use
pyupgradeorruff --target-version py313to modernise syntax mechanically. - Deploy to a canary, compare error rates and latency, then roll out and drop the old version from CI.
python
import warnings, datetime, sys, importlib.util
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
datetime.datetime.utcnow()
print(sys.version_info[:2], [w.category.__name__ for w in caught])
print(caught[0].message.args[0].split(".")[0])
print({m: importlib.util.find_spec(m) is not None for m in ["cgi", "imghdr", "telnetlib", "tomllib"]})
print(datetime.datetime.now(datetime.UTC).tzinfo)Run on Python 3.13.5, twice in fresh interpreters, with identical output.
Key points
- Enable warnings before upgrading
- Upgrade dependencies first
- Run CI on both versions
- Handle removed modules and deprecations
- Canary rollout with monitoring
Common mistakes
- Jumping versions without running tests with warnings on
- Upgrading Python before checking dependency support
Follow-ups
- How would you handle a dependency that is abandoned?
- What changed in asyncio between 3.8 and 3.13?
Tips
- Name concrete removals