Skip to content

Commit 1172536

Browse files
committed
Handle whitespaces for PEP-440 constraints
* fix incorrect parsing of spaces when parsing version constraints * add tests for `parse_constraint`
1 parent d39182a commit 1172536

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

poetry/core/semver/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def parse_constraint(constraints): # type: (str) -> VersionConstraint
2020
or_groups = []
2121
for constraints in or_constraints:
2222
and_constraints = re.split(
23-
"(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
23+
"(?<!^)(?<![~=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
2424
)
2525
constraint_objects = []
2626

poetry/core/semver/patterns.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
COMPLETE_VERSION = re.compile("(?i)" + _COMPLETE_VERSION)
1515

1616
CARET_CONSTRAINT = re.compile(r"(?i)^\^({})$".format(_COMPLETE_VERSION))
17-
TILDE_CONSTRAINT = re.compile("(?i)^~(?!=)({})$".format(_COMPLETE_VERSION))
18-
TILDE_PEP440_CONSTRAINT = re.compile("(?i)^~=({})$".format(_COMPLETE_VERSION))
17+
TILDE_CONSTRAINT = re.compile(r"(?i)^~(?!=)\s*({})$".format(_COMPLETE_VERSION))
18+
TILDE_PEP440_CONSTRAINT = re.compile(r"(?i)^~=\s*({})$".format(_COMPLETE_VERSION))
1919
X_CONSTRAINT = re.compile(r"^(!=|==)?\s*v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.[xX*])+$")
2020
BASIC_CONSTRAINT = re.compile(
2121
r"(?i)^(<>|!=|>=?|<=?|==?)?\s*({}|dev)".format(_COMPLETE_VERSION)

tests/semver/test_parse_constraint.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import pytest
2+
3+
from poetry.core.semver import Version
4+
from poetry.core.semver import VersionRange
5+
from poetry.core.semver import VersionUnion
6+
from poetry.core.semver import parse_constraint
7+
8+
9+
@pytest.mark.parametrize(
10+
"constraint,version",
11+
[
12+
("~=3.8", VersionRange(min=Version(3, 8), max=Version(4, 0), include_min=True)),
13+
(
14+
"~= 3.8",
15+
VersionRange(min=Version(3, 8), max=Version(4, 0), include_min=True),
16+
),
17+
("~3.8", VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True)),
18+
("~ 3.8", VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True)),
19+
(">3.8", VersionRange(min=Version(3, 8))),
20+
(">=3.8", VersionRange(min=Version(3, 8), include_min=True)),
21+
(">= 3.8", VersionRange(min=Version(3, 8), include_min=True)),
22+
(
23+
">3.8,<=6.5",
24+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
25+
),
26+
(
27+
">3.8,<= 6.5",
28+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
29+
),
30+
(
31+
"> 3.8,<= 6.5",
32+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
33+
),
34+
(
35+
"> 3.8,<=6.5",
36+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
37+
),
38+
(
39+
">3.8 ,<=6.5",
40+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
41+
),
42+
(
43+
">3.8, <=6.5",
44+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
45+
),
46+
(
47+
">3.8 , <=6.5",
48+
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
49+
),
50+
(
51+
"==3.8",
52+
VersionRange(
53+
min=Version(3, 8), max=Version(3, 8), include_min=True, include_max=True
54+
),
55+
),
56+
(
57+
"== 3.8",
58+
VersionRange(
59+
min=Version(3, 8), max=Version(3, 8), include_min=True, include_max=True
60+
),
61+
),
62+
(
63+
"~2.7 || ~3.8",
64+
VersionUnion(
65+
VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True),
66+
VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True),
67+
),
68+
),
69+
(
70+
"~2.7||~3.8",
71+
VersionUnion(
72+
VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True),
73+
VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True),
74+
),
75+
),
76+
(
77+
"~ 2.7||~ 3.8",
78+
VersionUnion(
79+
VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True),
80+
VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True),
81+
),
82+
),
83+
],
84+
)
85+
def test_parse_constraint(constraint, version):
86+
assert parse_constraint(constraint) == version

0 commit comments

Comments
 (0)