Skip to content

Commit

Permalink
constraints: handle pre-release dev tags
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Apr 19, 2022
1 parent d487e8b commit c0e7a91
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/poetry/core/packages/constraints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ def parse_single_constraint(constraint: str) -> Constraint:
m = BASIC_CONSTRAINT.match(constraint)
if m:
op = m.group(1)

if op is None:
op = "=="
version = constraint
else:
version = f"{constraint.strip()[len(op.strip()):]}"

version = m.group(2).strip()

return Constraint(version, op)
return Constraint(version.strip(), op)

raise ValueError(f"Could not parse version constraint: {constraint}")

Expand Down
20 changes: 12 additions & 8 deletions src/poetry/core/semver/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,18 @@ def parse_single_constraint(constraint: str) -> VersionConstraint:
m = BASIC_CONSTRAINT.match(constraint)
if m:
op = m.group(1)
version_string = m.group(2)

# Technically invalid constraints like `>= 3.*` will appear
# here as `3.`.
# Pip currently supports these and to avoid breaking existing
# users workflows we need to support them as well. To do so,
# we just remove the inconsequential part.
version_string = version_string.rstrip(".")
version_string = (
f"{constraint.strip()[len(op.strip()):]}" if op else constraint.strip()
)

if version_string.endswith(".*"):
# Technically invalid constraints like `>= 3.*` will appear
# here as `3.*`.
#
# Pip currently supports these and to avoid breaking existing
# users workflows we need to support them as well. To do so,
# we just remove the inconsequential part.
version_string = version_string[:-2]

if version_string == "dev":
version_string = "0.0-dev"
Expand Down
32 changes: 32 additions & 0 deletions tests/semver/test_parse_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@
include_min=True,
),
),
(
"^1.0.0a1.dev0",
VersionRange(
min=Version.from_parts(
1, 0, 0, pre=ReleaseTag("a", 1), dev=ReleaseTag("dev", 0)
),
max=Version.from_parts(2, 0, 0),
include_min=True,
),
),
(
"1.0.0a1.dev0",
VersionRange(
min=Version.from_parts(
1, 0, 0, pre=ReleaseTag("a", 1), dev=ReleaseTag("dev", 0)
),
max=Version.from_parts(
1, 0, 0, pre=ReleaseTag("a", 1), dev=ReleaseTag("dev", 0)
),
include_min=True,
),
),
(
"~1.0.0a1",
VersionRange(
Expand All @@ -190,6 +212,16 @@
include_min=True,
),
),
(
"~1.0.0a1.dev0",
VersionRange(
min=Version.from_parts(
1, 0, 0, pre=ReleaseTag("a", 1), dev=ReleaseTag("dev", 0)
),
max=Version.from_parts(1, 1, 0),
include_min=True,
),
),
(
"^0",
VersionRange(
Expand Down
1 change: 1 addition & 0 deletions tests/version/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def assert_requirement(
("name<3.*", {"name": "name", "constraint": "<3.0"}),
("name>3.5.*", {"name": "name", "constraint": ">3.5"}),
("name==1.0.post1", {"name": "name", "constraint": "==1.0.post1"}),
("name==1.2.0b1.dev0", {"name": "name", "constraint": "==1.2.0b1.dev0"}),
(
"name>=1.2.3;python_version=='2.6'",
{
Expand Down

0 comments on commit c0e7a91

Please sign in to comment.