YoavKlein
YoavKlein

Reputation: 2705

How can I know what Python versions can run my code?

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...?

Upvotes: 4

Views: 7403

Answers (3)

user23952
user23952

Reputation: 682

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.

Upvotes: 2

Kirk Strauser
Kirk Strauser

Reputation: 30933

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.

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155313

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.

Upvotes: 8

Related Questions