Skip to content

Commit 96e7be6

Browse files
mayeutKOLANICH
authored andcommitted
Fix prefix version matching (pypa#564)
0-padding shall only be applied on the prospective version, before shortening, in order to get the correct shortened prospective version.
1 parent 879bb94 commit 96e7be6

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packaging/specifiers.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def _compare_equal(self, prospective: Version, spec: str) -> bool:
380380
# We need special logic to handle prefix matching
381381
if spec.endswith(".*"):
382382
# In the case of prefix matching we want to ignore local segment.
383-
prospective = Version(prospective.public)
383+
normalized_prospective = canonicalize_version(prospective.public)
384384
# Get the normalized version string ignoring the trailing .*
385385
normalized_spec = canonicalize_version(spec[:-2], strip_trailing_zero=False)
386386
# Split the spec out by dots, and pretend that there is an implicit
@@ -390,20 +390,18 @@ def _compare_equal(self, prospective: Version, spec: str) -> bool:
390390
# Split the prospective version out by dots, and pretend that there
391391
# is an implicit dot in between a release segment and a pre-release
392392
# segment.
393-
split_prospective = _version_split(str(prospective))
393+
split_prospective = _version_split(normalized_prospective)
394+
395+
# 0-pad the prospective version before shortening it to get the correct
396+
# shortened version.
397+
padded_prospective, _ = _pad_version(split_prospective, split_spec)
394398

395399
# Shorten the prospective version to be the same length as the spec
396400
# so that we can determine if the specifier is a prefix of the
397401
# prospective version or not.
398-
shortened_prospective = split_prospective[: len(split_spec)]
399-
400-
# Pad out our two sides with zeros so that they both equal the same
401-
# length.
402-
padded_spec, padded_prospective = _pad_version(
403-
split_spec, shortened_prospective
404-
)
402+
shortened_prospective = padded_prospective[: len(split_spec)]
405403

406-
return padded_prospective == padded_spec
404+
return shortened_prospective == split_spec
407405
else:
408406
# Convert our spec string into a Version
409407
spec_version = Version(spec)

tests/test_specifiers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,11 @@ def test_comparison_non_specifier(self):
307307
("2b1.dev1", "==2.*"),
308308
("2c1", "==2.*"),
309309
("2c1.post1.dev1", "==2.*"),
310+
("2c1.post1.dev1", "==2.0.*"),
310311
("2rc1", "==2.*"),
312+
("2rc1", "==2.0.*"),
311313
("2", "==2.*"),
314+
("2", "==2.0.*"),
312315
("2", "==0!2.*"),
313316
("0!2", "==2.*"),
314317
("2.0", "==2.*"),
@@ -405,8 +408,11 @@ def test_comparison_non_specifier(self):
405408
("2b1.dev1", "!=2.*"),
406409
("2c1", "!=2.*"),
407410
("2c1.post1.dev1", "!=2.*"),
411+
("2c1.post1.dev1", "!=2.0.*"),
408412
("2rc1", "!=2.*"),
413+
("2rc1", "!=2.0.*"),
409414
("2", "!=2.*"),
415+
("2", "!=2.0.*"),
410416
("2.0", "!=2.*"),
411417
("2.0.0", "!=2.*"),
412418
# Test the greater than equal operation

0 commit comments

Comments
 (0)