Description
This guide has been written by @tonybaloney and can now be found here: https://packaging.python.org/guides/dropping-older-python-versions/
Talking to @tonybaloney about https://twitter.com/anthonypjshaw/status/970088412177776640, I discovered that our support for gracefully dropping support for older Python versions is currently better than I realised, but it's also far from obvious how to actually do it and the potential errors to avoid.
The key moving parts are:
- the
Requires-Python
field in metadata 1.2+: https://packaging.python.org/specifications/core-metadata/#requires-python - the
data-requires-python
link attribute in the index server release listing API: https://www.python.org/dev/peps/pep-0503/#specification - a
data-requires-python
aware installer (e.g.pip
9+) - a publishing client that allows setting the
Requires-Python
field in the project metadata (e.g. thepython_requires
setting insetuptools
24.2.0+) - an index server that sets
data-requires-python
correctly (e.g.pypi.python.org
since pypi/legacy@3e1ce8f)
The most conservative approach to implicitly pinning older clients to the last compatible release would be to release an alpha or development release that sets "Requires-Python: >= 3.6" (for example) in the metadata, and includes an import-time warning snippet like the one below, but doesn't actually require the new version yet:
_MINIMUM_SUPPORTED_VERSION = (3, 6, 0) # f-strings! Async generators!
if sys.version_info < _MINIMUM_SUPPORTED_VERSION:
import warnings
warnings.warn("This project no longer supports this version of Python, "
"but the release metadata indicating that has been ignored. "
"Please update to a newer installation client (e.g. pip 9+) and "
"ensure that any caching proxies in use support PEP 503's"
"`data-requires-python` link attribute.", FutureWarning)
(Using FutureWarning
ensures that the message will be visible by default even on versions where DeprecationWarning
is hidden by default)
This conservative approach has a few key benefits:
- It allows for opt-in testing of the metadata setting by folks running
pip install --pre
while others on recentpip
versions will ignore it even ifRequires-Python
didn't get set correctly (since it's a pre-release) - It means that folks with older deployment toolchains (even truly ancient ones that allow installation of pre-releases by default) will only get a (valid!) warning on stderr instead of a completely broken deployment.
- It means that if something does go wrong with setting
Requires-Python
in the metadata, you only end up with a pre-release that emits a spurious warning rather than one that's fundamentally broken, and can easily push a new version that sets that metadata correctly (since older clients should be updated to versions that default to ignoring pre-releases).