I've read in few places that generally, Python doesn't provide backward compatibility, which means that any newer version of Python may break code that worked fine for earlier versions. If so, what is my way as a developer to know what versions of Python can execute my code successfully? Is there any set of rules/guarantees regarding this? Or should I just tell my users: Just run this with Python 3.8 (for example) - no more no less...?
3 Answers
99% of the time, if it works on Python 3.x, it'll work on 3.y where y >= x. Enabling warnings when running your code on the older version should pop DeprecationWarnings when you use a feature that's deprecated (and therefore likely to change/be removed in later Python versions). Aside from that, you can read the What's New docs for each version between the known good version and the later versions, in particular the Deprecated and Removed sections of each.
Beyond that, the only solution is good unit and component tests (you are using those, right? 😉) that you rerun on newer releases to verify stuff still works & behavior doesn't change.
7 Comments
DeprecationWarnings for features that will change or go away in newer versions. If your code is relying on deprecated features in older versions, you'll want to change it now, so it works in the newer versions too. Moving your testing forward a minor version at a time will help find any/all deprecated features (assuming said deprecations were tagged with warnings; not all can be).DeprecationWarning can't be raised). That said, nothing will guarantee safety save proper testing, sorry.According to PEP387, section "Making Incompatible Changes", before incompatible changes are made, a deprecation warning should appear in at least two minor Python versions of the same major version, or one minor version in an older major version. After that, it's a free game, in principle. This made me cringe with regards to safety. Who knows if people run airplanes on Python and if they don't always read the python-dev list. So if you have something that passes 100% coverage unit tests without deprecation warnings, your code should be safe for the next two minor releases.
You can avoid this issue and many others by containerizing your deployments.
1 Comment
tox is great for running unit tests against multiple Python versions. That’s useful for at least 2 major cases:
- You want to ensure compatibility for a certain set of Python versions, say 3.7+, and to be told if you make any breaking changes.
- You don’t really know what versions your code supports, but want to establish a baseline of supported versions for future work.
I don’t use it for internal projects where I can control over the environment where my code will be running. It’s lovely for people publishing apps or libraries to PyPI, though.
math.product, for instance, is post-3.7.varsonnamedtuples). Another example wasasyncandawaitbecoming keywords in 3.7 IIRC (which broke thepikapackage, where0.11usedasyncas a variable name, and required the0.12release ofpika).