Skip to content

Commit 2b05e8e

Browse files
mnriemCopilot
andcommitted
fix(bundler): normalize prerelease spellings inside version constraints
Addresses review 4535327154: parse_version() normalized SemVer prerelease spellings (e.g. 1.2.3-rc1 -> 1.2.3rc1) but parse_constraint() passed the constraint to packaging.SpecifierSet unmodified, so ">=1.2.3-rc1" raised InvalidSpecifier even though the same spelling is accepted for installed versions. parse_constraint() now normalizes the version portion of each comma-separated clause via the shared _normalize_semver helper, so prerelease handling is consistent across versions and constraints. Co-authored-by: Copilot <[email protected]>
1 parent 0eabe7a commit 2b05e8e

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/specify_cli/bundler/lib/versioning.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,31 @@ def parse_version(value: str) -> Version:
3838
raise BundlerError(f"Invalid version '{value}': {exc}") from exc
3939

4040

41+
_SPECIFIER_CLAUSE = re.compile(r"^\s*(===|==|~=|!=|<=|>=|<|>)?\s*(.*?)\s*$")
42+
43+
44+
def _normalize_constraint(value: str) -> str:
45+
"""Normalize the version portion of each clause in a constraint string.
46+
47+
``packaging.SpecifierSet`` rejects SemVer prerelease spellings like
48+
``>=1.2.3-rc1`` verbatim, even though :func:`parse_version` accepts the same
49+
spelling for installed versions. Normalize each comma-separated clause's
50+
version so prerelease handling is consistent across versions and constraints.
51+
"""
52+
clauses = []
53+
for raw in str(value).split(","):
54+
if not raw.strip():
55+
continue
56+
match = _SPECIFIER_CLAUSE.match(raw)
57+
operator, version = match.groups()
58+
clauses.append(f"{operator or ''}{_normalize_semver(version)}")
59+
return ",".join(clauses)
60+
61+
4162
def parse_constraint(value: str) -> SpecifierSet:
4263
"""Parse a version constraint such as ``>=0.9.0`` into a :class:`SpecifierSet`."""
4364
try:
44-
return SpecifierSet(str(value))
65+
return SpecifierSet(_normalize_constraint(value))
4566
except InvalidSpecifier as exc:
4667
raise BundlerError(
4768
f"Invalid version constraint '{value}': {exc}"

‎tests/unit/test_bundler_versioning.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,21 @@ def test_uppercase_v_prefix_tolerated():
4848
# Mirrors specify_cli._version tag normalization (V -> v).
4949
assert is_semver("V1.2.3") is True
5050
assert satisfies("V1.2.3", ">=1.2.0") is True
51+
52+
53+
@pytest.mark.parametrize("installed,constraint,ok", [
54+
# Prerelease spellings are now normalized inside constraints too, so a
55+
# constraint like ">=1.2.3-rc1" parses (previously raised InvalidSpecifier).
56+
("1.2.3-rc2", ">=1.2.3-rc1", True),
57+
("1.2.2", ">=1.2.3-rc1", False),
58+
("1.5.0", ">=1.2.3-rc1,<2.0.0", True),
59+
("1.2.3-beta.1", ">=1.2.3-alpha1", True),
60+
])
61+
def test_satisfies_prerelease_in_constraint(installed, constraint, ok):
62+
assert satisfies(installed, constraint) is ok
63+
64+
65+
def test_parse_constraint_empty_is_permissive():
66+
from specify_cli.bundler.lib.versioning import parse_constraint
67+
68+
assert str(parse_constraint("")) == ""

0 commit comments

Comments
 (0)