Skip to content

Commit

Permalink
Fix LEQ/GEQ for SpecifierSets when considering versions with a local …
Browse files Browse the repository at this point in the history
…version label (#304)

* Add failing test

* Ignore local version when comparing

PEP 440 specifies:

"If the specified version identifier is a public version identifier (no
local version label), then the local version label of any candidate
versions MUST be ignored when matching versions."

* Add comments
  • Loading branch information
di committed May 13, 2020
1 parent fbe5144 commit db291c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,20 @@ def _compare_not_equal(self, prospective, spec):
@_require_version_compare
def _compare_less_than_equal(self, prospective, spec):
# type: (ParsedVersion, str) -> bool
return prospective <= Version(spec)

# NB: Local version identifiers are NOT permitted in the version
# specifier, so local version labels can be universally removed from
# the prospective version.
return Version(prospective.public) <= Version(spec)

@_require_version_compare
def _compare_greater_than_equal(self, prospective, spec):
# type: (ParsedVersion, str) -> bool
return prospective >= Version(spec)

# NB: Local version identifiers are NOT permitted in the version
# specifier, so local version labels can be universally removed from
# the prospective version.
return Version(prospective.public) >= Version(spec)

@_require_version_compare
def _compare_less_than(self, prospective, spec_str):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,3 +976,17 @@ def test_comparison_canonicalizes(self, left, right):
def test_comparison_non_specifier(self):
assert SpecifierSet("==1.0") != 12
assert not SpecifierSet("==1.0") == 12

@pytest.mark.parametrize(
("version", "specifier", "expected"),
[
("1.0.0+local", "==1.0.0", True),
("1.0.0+local", "!=1.0.0", False),
("1.0.0+local", "<=1.0.0", True),
("1.0.0+local", ">=1.0.0", True),
("1.0.0+local", "<1.0.0", False),
("1.0.0+local", ">1.0.0", False),
],
)
def test_comparison_ignores_local(self, version, specifier, expected):
assert (Version(version) in SpecifierSet(specifier)) == expected

0 comments on commit db291c7

Please sign in to comment.