Skip to content

Commit

Permalink
Merge pull request #36 from python-poetry/backport-markers-fixes
Browse files Browse the repository at this point in the history
Fix handling of dependency markers with python precision >= 3
  • Loading branch information
sdispater authored Jun 9, 2020
2 parents 933dbf4 + 331fb5b commit d582844
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
21 changes: 19 additions & 2 deletions poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,27 +271,44 @@ def _create_nested_marker(self, name, constraint):

marker = glue.join(parts)
elif isinstance(constraint, Version):
if constraint.precision >= 3 and name == "python_version":
name = "python_full_version"

marker = '{} == "{}"'.format(name, constraint.text)
else:
if constraint.min is not None:
min_name = name
if constraint.min.precision >= 3 and name == "python_version":
min_name = "python_full_version"

if constraint.max is None:
name = min_name

op = ">="
if not constraint.include_min:
op = ">"

version = constraint.min.text
if constraint.max is not None:
text = '{} {} "{}"'.format(name, op, version)
max_name = name
if constraint.max.precision >= 3 and name == "python_version":
max_name = "python_full_version"

text = '{} {} "{}"'.format(min_name, op, version)

op = "<="
if not constraint.include_max:
op = "<"

version = constraint.max

text += ' and {} {} "{}"'.format(name, op, version)
text += ' and {} {} "{}"'.format(max_name, op, version)

return text
elif constraint.max is not None:
if constraint.max.precision >= 3 and name == "python_version":
name = "python_full_version"

op = "<="
if not constraint.include_max:
op = "<"
Expand Down
6 changes: 5 additions & 1 deletion poetry/core/version/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ def __eq__(self, other):
class SingleMarker(BaseMarker):

_CONSTRAINT_RE = re.compile(r"(?i)^(~=|!=|>=?|<=?|==?=?|in|not in)?\s*(.+)$")
_VERSION_LIKE_MARKER_NAME = {"python_version", "platform_release"}
_VERSION_LIKE_MARKER_NAME = {
"python_version",
"python_full_version",
"platform_release",
}

def __init__(self, name, constraint):
from poetry.core.packages.constraints import (
Expand Down
22 changes: 22 additions & 0 deletions tests/packages/test_dependency.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from poetry.core.packages import Dependency
from poetry.core.packages import Package

Expand Down Expand Up @@ -108,3 +110,23 @@ def test_to_pep_508_with_single_version_excluded():
dependency = Dependency("foo", "!=1.2.3")

assert "foo (!=1.2.3)" == dependency.to_pep_508()


@pytest.mark.parametrize(
"python_versions, marker",
[
(">=3.5,<3.5.4", 'python_version >= "3.5" and python_full_version < "3.5.4"'),
(">=3.5.4,<3.6", 'python_full_version >= "3.5.4" and python_version < "3.6"'),
("<3.5.4", 'python_full_version < "3.5.4"'),
(">=3.5.4", 'python_full_version >= "3.5.4"'),
("== 3.5.4", 'python_full_version == "3.5.4"'),
],
)
def test_to_pep_508_with_patch_python_version(python_versions, marker):
dependency = Dependency("Django", "^1.23")
dependency.python_versions = python_versions

expected = "Django (>=1.23,<2.0); {}".format(marker)

assert expected == dependency.to_pep_508()
assert marker == str(dependency.marker)
18 changes: 18 additions & 0 deletions tests/packages/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,21 @@ def test_dependency_from_pep_508_with_wheel_url():

assert "example-wheel" == dep.name
assert str(dep.constraint) == "14.0.2"


def test_dependency_from_pep_508_with_python_full_version():
name = (
"requests (==2.18.0); "
'(python_version >= "2.7" and python_version < "2.8") '
'or (python_full_version >= "3.4" and python_full_version < "3.5.4")'
)
dep = dependency_from_pep_508(name)

assert dep.name == "requests"
assert str(dep.constraint) == "2.18.0"
assert dep.extras == []
assert dep.python_versions == ">=2.7 <2.8 || >=3.4 <3.5.4"
assert str(dep.marker) == (
'python_version >= "2.7" and python_version < "2.8" '
'or python_full_version >= "3.4" and python_full_version < "3.5.4"'
)

0 comments on commit d582844

Please sign in to comment.