From ea6f93afe21f5863c220cb38891f11a5d236ebfc Mon Sep 17 00:00:00 2001 From: mayeut Date: Sat, 18 Jun 2022 12:36:47 +0200 Subject: [PATCH] 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. --- packaging/specifiers.py | 4 +++- tests/test_specifiers.py | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packaging/specifiers.py b/packaging/specifiers.py index d2ba13d10..bbd641752 100644 --- a/packaging/specifiers.py +++ b/packaging/specifiers.py @@ -386,9 +386,11 @@ def _compare_equal(self, prospective: Version, spec: str) -> bool: if spec.endswith(".*"): # In the case of prefix matching we want to ignore local segment. prospective = Version(prospective.public) + # Get the normalized version string ignoring the trailing .* + normalized_spec = canonicalize_version(spec[:-2], strip_trailing_zero=False) # Split the spec out by dots, and pretend that there is an implicit # dot in between a release segment and a pre-release segment. - split_spec = _version_split(spec[:-2]) # Remove the trailing .* + split_spec = _version_split(normalized_spec) # Split the prospective version out by dots, and pretend that there # is an implicit dot in between a release segment and a pre-release diff --git a/tests/test_specifiers.py b/tests/test_specifiers.py index 043dd0652..a5cb9ce01 100644 --- a/tests/test_specifiers.py +++ b/tests/test_specifiers.py @@ -309,6 +309,8 @@ def test_comparison_non_specifier(self): ("2c1.post1.dev1", "==2.*"), ("2rc1", "==2.*"), ("2", "==2.*"), + ("2", "==0!2.*"), + ("0!2", "==2.*"), ("2.0", "==2.*"), ("2.0.0", "==2.*"), ("2.1+local.version", "==2.1.*"), @@ -358,6 +360,7 @@ def test_comparison_non_specifier(self): ("1.1", "~=1.0"), ("1.9999999", "~=1.0"), ("1.1", "~=1.0a1"), + ("2022.01.01", "~=2022.01.01"), # Test that epochs are handled sanely ("2!1.0", "~=2!1.0"), ("2!1.0", "==2!1.*"),