Skip to content

Commit 2d38992

Browse files
committed
Normalize specifier version for prefix matching
In case of prefix version matching, normalization of the version was not applied to the specifier's version. This resulted in `2` being rejected as a candidate for `==0!2.*` With normalization applied, the candidate is now accepted.
1 parent 9385276 commit 2d38992

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

packaging/specifiers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,11 @@ def _compare_equal(self, prospective: Version, spec: str) -> bool:
386386
if spec.endswith(".*"):
387387
# In the case of prefix matching we want to ignore local segment.
388388
prospective = Version(prospective.public)
389+
# Get the normalized version string ignoring the trailing .*
390+
normalized_spec = canonicalize_version(spec[:-2], strip_trailing_zero=False)
389391
# Split the spec out by dots, and pretend that there is an implicit
390392
# dot in between a release segment and a pre-release segment.
391-
split_spec = _version_split(spec[:-2]) # Remove the trailing .*
393+
split_spec = _version_split(normalized_spec)
392394

393395
# Split the prospective version out by dots, and pretend that there
394396
# is an implicit dot in between a release segment and a pre-release

tests/test_specifiers.py

+5
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ def test_comparison_non_specifier(self):
302302
("2c1.post1.dev1", "==2.*"),
303303
("2rc1", "==2.*"),
304304
("2", "==2.*"),
305+
("2", "==0!2.*"),
306+
("0!2", "==2.*"),
305307
("2.0", "==2.*"),
306308
("2.0.0", "==2.*"),
307309
("2.0.post1", "==2.0.post1.*"),
308310
("2.0.post1.dev1", "==2.0.post1.*"),
311+
("2.0.rc1", "==2.0.c1.*"),
312+
("2.0.rc1.post1", "==2.0.c1.*"),
309313
("2.1+local.version", "==2.1.*"),
310314
# Test the in-equality operation
311315
("2.1", "!=2"),
@@ -353,6 +357,7 @@ def test_comparison_non_specifier(self):
353357
("1.1", "~=1.0"),
354358
("1.9999999", "~=1.0"),
355359
("1.1", "~=1.0a1"),
360+
("2022.01.01", "~=2022.01.01"),
356361
# Test that epochs are handled sanely
357362
("2!1.0", "~=2!1.0"),
358363
("2!1.0", "==2!1.*"),

0 commit comments

Comments
 (0)