3

Just a quick question I stumbled upon. It's not really important, but I'm interested in the reason for this and haven't found a quick answer, yet.

According to PEP 508 -- Environment Markers (section "Specification" - "Environment markers") marker variable python_version is defined as platform.python_version()[:3].

So for Python 3.5.2, python_version is going to be 3.5.

However, what if either major or minor version number is higher than 9? The version number would consist of more than 3 characters and [:3] would crop it. Isn't this a possibility for future Python versions?

Why not use something like ".".join(platform.python_version().split(".", 2)[:2]) which still seems very basic and won't overly complicate the code?

finefoot
  • 9,914
  • 7
  • 59
  • 102

1 Answers1

1

This has now been corrected, as a follow-up to this question. See issue #560, pull request #1123 and the summary of changes in the updated PEP:

  • The definition of python_version was changed from platform.python_version()[:3] to '.'.join(platform.python_version_tuple()[:2], to accommodate potential future versions of Python with 2-digit major and minor versions (e.g. 3.10). [7]

The packaging library source code has followed suit.


Original answer

I'm sure it is just an oversight or simplification for documentation purposes. The intention is clearly to provide a major.minor version string.

Note that historically, there never has been a minor version number with double digits. So far, Python has managed with just one digit, always. The highest numbers, so far, have been 1.6, 2.7 and 3.7. That's not to say there never will be a 3.10 in future, but so far, it just hasn't come up.

I'd imagine an actual implementation to use platform.python_version_tuple():

return '.'.join(platform.python_version_tuple()[:2]

or

major, minor, patch = platform.python_version_tuple()
return '{}.{}'.format(major, minor)

However, both forms are rather a bit more verbose to fit into a PEP table column aiming to document rather than prescribe code.

If in doubt, write to the Python-dev mailing list. I'm sure that if this was to become so confusing as to be problematic, the PEP can be adjusted.

As for pip; it uses the vendored packaging library to handle environment markers, which has implemented the python_version marker verbatim as specced in the PEP. If you feel strongly about it, you could file an issue there, the PEP maintainers are active on that project.

Note that there is still plenty of time. So far, 3.x releases each take about 18 months to appear (3.2: 2011-02, 3.3: 2012-09, 3.4: 2014-03, 3.5: 2015-09, 3.6: 2016-12, 3.7: expected 2018-06), so we have 5 years or so to address this.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343