Skip to content

Commit

Permalink
Accept locally installed prereleases (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
q0w committed Mar 5, 2022
1 parent 75966a8 commit f61a11b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packaging/specifiers.py
Expand Up @@ -57,7 +57,8 @@ def __eq__(self, other: object) -> bool:
objects are equal.
"""

@abc.abstractproperty
@property
@abc.abstractmethod
def prereleases(self) -> Optional[bool]:
"""
Returns whether or not pre-releases as a whole are allowed by this
Expand Down Expand Up @@ -724,7 +725,10 @@ def __contains__(self, item: UnparsedVersion) -> bool:
return self.contains(item)

def contains(
self, item: UnparsedVersion, prereleases: Optional[bool] = None
self,
item: UnparsedVersion,
prereleases: Optional[bool] = None,
installed: Optional[bool] = None,
) -> bool:

# Ensure that our item is a Version or LegacyVersion instance.
Expand All @@ -746,6 +750,9 @@ def contains(
if not prereleases and item.is_prerelease:
return False

if installed and item.is_prerelease:
item = parse(item.base_version)

# We simply dispatch to the underlying specs here to make sure that the
# given version is contained within all of them.
# Note: This use of all() here means that an empty set of specifiers
Expand Down
9 changes: 9 additions & 0 deletions tests/test_specifiers.py
Expand Up @@ -819,6 +819,15 @@ def test_specifier_contains_prereleases(self):
assert spec.contains("1.0.dev1")
assert not spec.contains("1.0.dev1", prereleases=False)

def test_specifier_contains_installed_prereleases(self):
spec = SpecifierSet("~=1.0")
assert not spec.contains("1.0.0.dev1", installed=True)
assert spec.contains("1.0.0.dev1", prereleases=True, installed=True)

spec = SpecifierSet("~=1.0", prereleases=True)
assert spec.contains("1.0.0.dev1", installed=True)
assert not spec.contains("1.0.0.dev1", prereleases=False, installed=False)

@pytest.mark.parametrize(
("specifier", "specifier_prereleases", "prereleases", "input", "expected"),
[
Expand Down

0 comments on commit f61a11b

Please sign in to comment.