Skip to content

Commit

Permalink
Handle whitespaces for PEP-440 constraints
Browse files Browse the repository at this point in the history
* fix incorrect parsing of spaces when parsing version constraints
* add tests for `parse_constraint`
  • Loading branch information
abn committed Apr 25, 2020
1 parent 9a28396 commit 9f93df1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
2 changes: 1 addition & 1 deletion poetry/semver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def parse_constraint(constraints): # type: (str) -> VersionConstraint
or_groups = []
for constraints in or_constraints:
and_constraints = re.split(
"(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
"(?<!^)(?<![~=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints
)
constraint_objects = []

Expand Down
4 changes: 2 additions & 2 deletions poetry/semver/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
COMPLETE_VERSION = re.compile("(?i)" + _COMPLETE_VERSION)

CARET_CONSTRAINT = re.compile(r"(?i)^\^({})$".format(_COMPLETE_VERSION))
TILDE_CONSTRAINT = re.compile("(?i)^~(?!=)({})$".format(_COMPLETE_VERSION))
TILDE_PEP440_CONSTRAINT = re.compile("(?i)^~=({})$".format(_COMPLETE_VERSION))
TILDE_CONSTRAINT = re.compile(r"(?i)^~(?!=)\s*({})$".format(_COMPLETE_VERSION))
TILDE_PEP440_CONSTRAINT = re.compile(r"(?i)^~=\s*({})$".format(_COMPLETE_VERSION))
X_CONSTRAINT = re.compile(r"^(!=|==)?\s*v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.[xX*])+$")
BASIC_CONSTRAINT = re.compile(
r"(?i)^(<>|!=|>=?|<=?|==?)?\s*({}|dev)".format(_COMPLETE_VERSION)
Expand Down
86 changes: 86 additions & 0 deletions tests/semver/test_parse_constraint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import pytest

from poetry.semver import Version
from poetry.semver import VersionRange
from poetry.semver import VersionUnion
from poetry.semver import parse_constraint


@pytest.mark.parametrize(
"constraint,version",
[
("~=3.8", VersionRange(min=Version(3, 8), max=Version(4, 0), include_min=True)),
(
"~= 3.8",
VersionRange(min=Version(3, 8), max=Version(4, 0), include_min=True),
),
("~3.8", VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True)),
("~ 3.8", VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True)),
(">3.8", VersionRange(min=Version(3, 8))),
(">=3.8", VersionRange(min=Version(3, 8), include_min=True)),
(">= 3.8", VersionRange(min=Version(3, 8), include_min=True)),
(
">3.8,<=6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
">3.8,<= 6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
"> 3.8,<= 6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
"> 3.8,<=6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
">3.8 ,<=6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
">3.8, <=6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
">3.8 , <=6.5",
VersionRange(min=Version(3, 8), max=Version(6, 5), include_max=True),
),
(
"==3.8",
VersionRange(
min=Version(3, 8), max=Version(3, 8), include_min=True, include_max=True
),
),
(
"== 3.8",
VersionRange(
min=Version(3, 8), max=Version(3, 8), include_min=True, include_max=True
),
),
(
"~2.7 || ~3.8",
VersionUnion(
VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True),
VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True),
),
),
(
"~2.7||~3.8",
VersionUnion(
VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True),
VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True),
),
),
(
"~ 2.7||~ 3.8",
VersionUnion(
VersionRange(min=Version(2, 7), max=Version(2, 8), include_min=True),
VersionRange(min=Version(3, 8), max=Version(3, 9), include_min=True),
),
),
],
)
def test_parse_constraint(constraint, version):
assert parse_constraint(constraint) == version

0 comments on commit 9f93df1

Please sign in to comment.