Filter items in the given iterable, that match the specifiers in this set. :param iterable: An iterable that can contain version strings and :class:`Version` instances. The items in the iterable will be filtered according to the specifier. :param prereleases:
(
self, iterable: Iterable[UnparsedVersionVar], prereleases: Optional[bool] = None
)
| 951 | return all(s.contains(item, prereleases=prereleases) for s in self._specs) |
| 952 | |
| 953 | def filter( |
| 954 | self, iterable: Iterable[UnparsedVersionVar], prereleases: Optional[bool] = None |
| 955 | ) -> Iterator[UnparsedVersionVar]: |
| 956 | """Filter items in the given iterable, that match the specifiers in this set. |
| 957 | |
| 958 | :param iterable: |
| 959 | An iterable that can contain version strings and :class:`Version` instances. |
| 960 | The items in the iterable will be filtered according to the specifier. |
| 961 | :param prereleases: |
| 962 | Whether or not to allow prereleases in the returned iterator. If set to |
| 963 | ``None`` (the default), it will be intelligently decide whether to allow |
| 964 | prereleases or not (based on the :attr:`prereleases` attribute, and |
| 965 | whether the only versions matching are prereleases). |
| 966 | |
| 967 | This method is smarter than just ``filter(SpecifierSet(...).contains, [...])`` |
| 968 | because it implements the rule from :pep:`440` that a prerelease item |
| 969 | SHOULD be accepted if no other versions match the given specifier. |
| 970 | |
| 971 | >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.3", "1.5a1"])) |
| 972 | ['1.3'] |
| 973 | >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.3", Version("1.4")])) |
| 974 | ['1.3', <Version('1.4')>] |
| 975 | >>> list(SpecifierSet(">=1.2.3").filter(["1.2", "1.5a1"])) |
| 976 | [] |
| 977 | >>> list(SpecifierSet(">=1.2.3").filter(["1.3", "1.5a1"], prereleases=True)) |
| 978 | ['1.3', '1.5a1'] |
| 979 | >>> list(SpecifierSet(">=1.2.3", prereleases=True).filter(["1.3", "1.5a1"])) |
| 980 | ['1.3', '1.5a1'] |
| 981 | |
| 982 | An "empty" SpecifierSet will filter items based on the presence of prerelease |
| 983 | versions in the set. |
| 984 | |
| 985 | >>> list(SpecifierSet("").filter(["1.3", "1.5a1"])) |
| 986 | ['1.3'] |
| 987 | >>> list(SpecifierSet("").filter(["1.5a1"])) |
| 988 | ['1.5a1'] |
| 989 | >>> list(SpecifierSet("", prereleases=True).filter(["1.3", "1.5a1"])) |
| 990 | ['1.3', '1.5a1'] |
| 991 | >>> list(SpecifierSet("").filter(["1.3", "1.5a1"], prereleases=True)) |
| 992 | ['1.3', '1.5a1'] |
| 993 | """ |
| 994 | # Determine if we're forcing a prerelease or not, if we're not forcing |
| 995 | # one for this particular filter call, then we'll use whatever the |
| 996 | # SpecifierSet thinks for whether or not we should support prereleases. |
| 997 | if prereleases is None: |
| 998 | prereleases = self.prereleases |
| 999 | |
| 1000 | # If we have any specifiers, then we want to wrap our iterable in the |
| 1001 | # filter method for each one, this will act as a logical AND amongst |
| 1002 | # each specifier. |
| 1003 | if self._specs: |
| 1004 | for spec in self._specs: |
| 1005 | iterable = spec.filter(iterable, prereleases=bool(prereleases)) |
| 1006 | return iter(iterable) |
| 1007 | # If we do not have any specifiers, then we need to have a rough filter |
| 1008 | # which will filter out any pre-releases, unless there are no final |
| 1009 | # releases. |
| 1010 | else: |
nothing calls this directly
no test coverage detected