Skip to content

Commit dcc2e40

Browse files
authored
Fix handling of dependency markers with python precision >= 3 (#2526)
1 parent b6c5c1e commit dcc2e40

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

Diff for: poetry/packages/dependency.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -271,27 +271,44 @@ def _create_nested_marker(self, name, constraint):
271271

272272
marker = glue.join(parts)
273273
elif isinstance(constraint, Version):
274+
if constraint.precision >= 3 and name == "python_version":
275+
name = "python_full_version"
276+
274277
marker = '{} == "{}"'.format(name, constraint.text)
275278
else:
276279
if constraint.min is not None:
280+
min_name = name
281+
if constraint.min.precision >= 3 and name == "python_version":
282+
min_name = "python_full_version"
283+
284+
if constraint.max is None:
285+
name = min_name
286+
277287
op = ">="
278288
if not constraint.include_min:
279289
op = ">"
280290

281291
version = constraint.min.text
282292
if constraint.max is not None:
283-
text = '{} {} "{}"'.format(name, op, version)
293+
max_name = name
294+
if constraint.max.precision >= 3 and name == "python_version":
295+
max_name = "python_full_version"
296+
297+
text = '{} {} "{}"'.format(min_name, op, version)
284298

285299
op = "<="
286300
if not constraint.include_max:
287301
op = "<"
288302

289303
version = constraint.max
290304

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

293307
return text
294308
elif constraint.max is not None:
309+
if constraint.max.precision >= 3 and name == "python_version":
310+
name = "python_full_version"
311+
295312
op = "<="
296313
if not constraint.include_max:
297314
op = "<"

Diff for: poetry/version/markers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ def __eq__(self, other):
270270
class SingleMarker(BaseMarker):
271271

272272
_CONSTRAINT_RE = re.compile(r"(?i)^(~=|!=|>=?|<=?|==?|in|not in)?\s*(.+)$")
273-
_VERSION_LIKE_MARKER_NAME = {"python_version", "platform_release"}
273+
_VERSION_LIKE_MARKER_NAME = {
274+
"python_version",
275+
"python_full_version",
276+
"platform_release",
277+
}
274278

275279
def __init__(self, name, constraint):
276280
from poetry.packages.constraints import (

Diff for: tests/packages/test_dependency.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from poetry.packages import Dependency
24
from poetry.packages import Package
35

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

110112
assert "foo (!=1.2.3)" == dependency.to_pep_508()
113+
114+
115+
@pytest.mark.parametrize(
116+
"python_versions, marker",
117+
[
118+
(">=3.5,<3.5.4", 'python_version >= "3.5" and python_full_version < "3.5.4"'),
119+
(">=3.5.4,<3.6", 'python_full_version >= "3.5.4" and python_version < "3.6"'),
120+
("<3.5.4", 'python_full_version < "3.5.4"'),
121+
(">=3.5.4", 'python_full_version >= "3.5.4"'),
122+
("== 3.5.4", 'python_full_version == "3.5.4"'),
123+
],
124+
)
125+
def test_to_pep_508_with_patch_python_version(python_versions, marker):
126+
dependency = Dependency("Django", "^1.23")
127+
dependency.python_versions = python_versions
128+
129+
expected = "Django (>=1.23,<2.0); {}".format(marker)
130+
131+
assert expected == dependency.to_pep_508()
132+
assert marker == str(dependency.marker)

Diff for: tests/packages/test_main.py

+18
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,21 @@ def test_dependency_from_pep_508_with_wheel_url():
218218

219219
assert "example-wheel" == dep.name
220220
assert str(dep.constraint) == "14.0.2"
221+
222+
223+
def test_dependency_from_pep_508_with_python_full_version():
224+
name = (
225+
"requests (==2.18.0); "
226+
'(python_version >= "2.7" and python_version < "2.8") '
227+
'or (python_full_version >= "3.4" and python_full_version < "3.5.4")'
228+
)
229+
dep = dependency_from_pep_508(name)
230+
231+
assert dep.name == "requests"
232+
assert str(dep.constraint) == "2.18.0"
233+
assert dep.extras == []
234+
assert dep.python_versions == ">=2.7 <2.8 || >=3.4 <3.5.4"
235+
assert str(dep.marker) == (
236+
'python_version >= "2.7" and python_version < "2.8" '
237+
'or python_full_version >= "3.4" and python_full_version < "3.5.4"'
238+
)

0 commit comments

Comments
 (0)