Skip to content

Commit

Permalink
Fix Requires-Python environment marker mapping. (#1105)
Browse files Browse the repository at this point in the history
Previously `Requires-Python` metadata was unconditionally mapped to
`python_version` environment marker clauses. This was incorrect for any
`Requires-Python` clauses that included version information beyond
`X.Y` and prevented activation of requirements in PEX files at runtime
in some cases. A previously failing test is added for the pandas case
that revealed this issue.

Fixes #1017
  • Loading branch information
jsirois authored Nov 7, 2020
1 parent ff8a852 commit ef99275
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from pex.platforms import Platform
from pex.requirements import local_project_from_requirement, local_projects_from_requirement_file
from pex.third_party.packaging.markers import Marker
from pex.third_party.packaging.version import Version
from pex.third_party.packaging.version import parse as parse_version
from pex.third_party.pkg_resources import Distribution, Environment, Requirement
from pex.tracer import TRACER
from pex.util import CacheHelper
Expand Down Expand Up @@ -130,11 +132,22 @@ def to_requirement(self, dist):
# + https://www.python.org/dev/peps/pep-0508/#environment-markers
python_requires = dist_metadata.requires_python(dist)
if python_requires:

def choose_marker(version):
# type: (str) -> str
parsed_version = parse_version(version)
if type(parsed_version) != Version or len(parsed_version.release) > 2:
return "python_full_version"
else:
return "python_version"

markers.update(
Marker(python_version)
for python_version in sorted(
"python_version {operator} {version!r}".format(
operator=specifier.operator, version=specifier.version
"{marker} {operator} {version!r}".format(
marker=choose_marker(specifier.version),
operator=specifier.operator,
version=specifier.version,
)
for specifier in python_requires
)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2413,3 +2413,20 @@ def test_resolve_arbitrary_equality_issues_940():
stdout, returncode = run_simple_pex(pex_file, args=["-c", "import foo"])
assert returncode == 0
assert stdout == b""


def test_resolve_python_requires_full_version_issues_1017():
# type: () -> None
python36 = ensure_python_interpreter(PY36)
result = run_pex_command(
python=python36,
args=[
"pandas==1.0.5",
"--",
"-c",
"import pandas; print(pandas._version.get_versions()['version'])",
],
quiet=True,
)
result.assert_success()
assert "1.0.5" == result.output.strip()

0 comments on commit ef99275

Please sign in to comment.