Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing for specifiers.BaseSpecifier.filter() #643

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
import abc
import itertools
import re
from typing import Callable, Iterable, Iterator, List, Optional, Set, Tuple, Union
from typing import (
Callable,
Iterable,
Iterator,
List,
Optional,
Set,
Tuple,
TypeVar,
Union,
)

from .utils import canonicalize_version
from .version import Version

UnparsedVersion = Union[Version, str]
UnparsedVersionVar = TypeVar("UnparsedVersionVar", bound=UnparsedVersion)
CallableOperator = Callable[[Version, str], bool]


Expand Down Expand Up @@ -85,8 +96,8 @@ def contains(self, item: str, prereleases: Optional[bool] = None) -> bool:

@abc.abstractmethod
def filter(
self, iterable: Iterable[UnparsedVersion], prereleases: Optional[bool] = None
) -> Iterator[UnparsedVersion]:
self, iterable: Iterable[UnparsedVersionVar], prereleases: Optional[bool] = None
) -> Iterator[UnparsedVersionVar]:
"""
Takes an iterable of items and filters them so that only items which
are contained within this specifier are allowed in it.
Expand Down Expand Up @@ -565,8 +576,8 @@ def contains(
return operator_callable(normalized_item, self.version)

def filter(
self, iterable: Iterable[UnparsedVersion], prereleases: Optional[bool] = None
) -> Iterator[UnparsedVersion]:
self, iterable: Iterable[UnparsedVersionVar], prereleases: Optional[bool] = None
) -> Iterator[UnparsedVersionVar]:
"""Filter items in the given iterable, that match the specifier.

:param iterable:
Expand Down Expand Up @@ -915,8 +926,8 @@ def contains(
return all(s.contains(item, prereleases=prereleases) for s in self._specs)

def filter(
self, iterable: Iterable[UnparsedVersion], prereleases: Optional[bool] = None
) -> Iterator[UnparsedVersion]:
self, iterable: Iterable[UnparsedVersionVar], prereleases: Optional[bool] = None
) -> Iterator[UnparsedVersionVar]:
"""Filter items in the given iterable, that match the specifiers in this set.

:param iterable:
Expand Down Expand Up @@ -972,8 +983,8 @@ def filter(
# which will filter out any pre-releases, unless there are no final
# releases.
else:
filtered: List[UnparsedVersion] = []
found_prereleases: List[UnparsedVersion] = []
filtered: List[UnparsedVersionVar] = []
found_prereleases: List[UnparsedVersionVar] = []

for item in iterable:
parsed_version = _coerce_version(item)
Expand Down