Filter items in the given iterable, that match the specifier. :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
)
| 579 | return operator_callable(normalized_item, self.version) |
| 580 | |
| 581 | def filter( |
| 582 | self, iterable: Iterable[UnparsedVersionVar], prereleases: Optional[bool] = None |
| 583 | ) -> Iterator[UnparsedVersionVar]: |
| 584 | """Filter items in the given iterable, that match the specifier. |
| 585 | |
| 586 | :param iterable: |
| 587 | An iterable that can contain version strings and :class:`Version` instances. |
| 588 | The items in the iterable will be filtered according to the specifier. |
| 589 | :param prereleases: |
| 590 | Whether or not to allow prereleases in the returned iterator. If set to |
| 591 | ``None`` (the default), it will be intelligently decide whether to allow |
| 592 | prereleases or not (based on the :attr:`prereleases` attribute, and |
| 593 | whether the only versions matching are prereleases). |
| 594 | |
| 595 | This method is smarter than just ``filter(Specifier().contains, [...])`` |
| 596 | because it implements the rule from :pep:`440` that a prerelease item |
| 597 | SHOULD be accepted if no other versions match the given specifier. |
| 598 | |
| 599 | >>> list(Specifier(">=1.2.3").filter(["1.2", "1.3", "1.5a1"])) |
| 600 | ['1.3'] |
| 601 | >>> list(Specifier(">=1.2.3").filter(["1.2", "1.2.3", "1.3", Version("1.4")])) |
| 602 | ['1.2.3', '1.3', <Version('1.4')>] |
| 603 | >>> list(Specifier(">=1.2.3").filter(["1.2", "1.5a1"])) |
| 604 | ['1.5a1'] |
| 605 | >>> list(Specifier(">=1.2.3").filter(["1.3", "1.5a1"], prereleases=True)) |
| 606 | ['1.3', '1.5a1'] |
| 607 | >>> list(Specifier(">=1.2.3", prereleases=True).filter(["1.3", "1.5a1"])) |
| 608 | ['1.3', '1.5a1'] |
| 609 | """ |
| 610 | |
| 611 | yielded = False |
| 612 | found_prereleases = [] |
| 613 | |
| 614 | kw = {"prereleases": prereleases if prereleases is not None else True} |
| 615 | |
| 616 | # Attempt to iterate over all the values in the iterable and if any of |
| 617 | # them match, yield them. |
| 618 | for version in iterable: |
| 619 | parsed_version = _coerce_version(version) |
| 620 | |
| 621 | if self.contains(parsed_version, **kw): |
| 622 | # If our version is a prerelease, and we were not set to allow |
| 623 | # prereleases, then we'll store it for later in case nothing |
| 624 | # else matches this specifier. |
| 625 | if parsed_version.is_prerelease and not ( |
| 626 | prereleases or self.prereleases |
| 627 | ): |
| 628 | found_prereleases.append(version) |
| 629 | # Either this is not a prerelease, or we should have been |
| 630 | # accepting prereleases from the beginning. |
| 631 | else: |
| 632 | yielded = True |
| 633 | yield version |
| 634 | |
| 635 | # Now that we've iterated over everything, determine if we've yielded |
| 636 | # any values, and if we have not and we have any prereleases stored up |
| 637 | # then we will go ahead and yield the prereleases. |
| 638 | if not yielded and found_prereleases: |
nothing calls this directly
no test coverage detected