Repository: pypa/pip
File: src/pip/_internal/resolution/resolvelib/found_candidates.py (Python)

Code before:

def _deduplicated_by_version(candidates):
    # type: (Iterator[Candidate]) -> Iterator[Candidate]
    returned = set()  # type: Set[_BaseVersion]
    for candidate in candidates:
        if candidate.version in returned:
            continue
        returned.add(candidate.version)
        yield candidate


def _insert_installed(installed, others):
    # type: (Candidate, Iterator[Candidate]) -> Iterator[Candidate]
    """Iterator for ``FoundCandidates``.

    This iterator is used when the resolver prefers to upgrade an
    already-installed package. Candidates from index are returned in their
    normal ordering, except replaced when the version is already installed.

    The implementation iterates through and yields other candidates, but insert
    the installed candidate exactly once before we start yielding older or
    equivalent candidates, or after all other candidates if they are all newer.
    """
    installed_yielded = False
    for candidate in others:
        # If the installed candidate if better, yield it first.
        if not installed_yielded and installed.version >= candidate.version:
            yield installed
            installed_yielded = True
        yield candidate

    # If the installed candidate is older than all other candidates.
    if not installed_yielded:
        yield installed


class FoundCandidates(collections_abc.Sequence):
    """A lazy sequence to provide candidates to the resolver.

    The intended usage is to return this from `find_matches()` so the resolver
    can iterate through the sequence multiple times, but only access the index
    page when remote packages are actually needed. This improve performances
    when suitable candidates are already installed on disk.
    """
    def __init__(
        self,
        get_others,  # type: Callable[[], Iterator[Candidate]]
        installed,  # type: Optional[Candidate]
        prefers_installed,  # type: bool
    ):
        self._get_others = get_others

Review comment (suggestion):
```suggestion
        # If the installed candidate is better, yield it first.
```

Code after:

from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
    from typing import Callable, Iterator, Optional, Set

    from pip._vendor.packaging.version import _BaseVersion

    from .base import Candidate


def _deduplicated_by_version(candidates):
    # type: (Iterator[Candidate]) -> Iterator[Candidate]
    returned = set()  # type: Set[_BaseVersion]
    for candidate in candidates:
        if candidate.version in returned:
            continue
        returned.add(candidate.version)
        yield candidate


def _insert_installed(installed, others):
    # type: (Candidate, Iterator[Candidate]) -> Iterator[Candidate]
    """Iterator for ``FoundCandidates``.

    This iterator is used when the resolver prefers to upgrade an
    already-installed package. Candidates from index are returned in their
    normal ordering, except replaced when the version is already installed.

    The implementation iterates through and yields other candidates, inserting
    the installed candidate exactly once before we start yielding older or
    equivalent candidates, or after all other candidates if they are all newer.
    """
    installed_yielded = False
    for candidate in others:
        # If the installed candidate is better, yield it first.
        if not installed_yielded and installed.version >= candidate.version:
            yield installed
            installed_yielded = True
        yield candidate

    # If the installed candidate is older than all other candidates.
    if not installed_yielded:
        yield installed


class FoundCandidates(collections_abc.Sequence):
    """A lazy sequence to provide candidates to the resolver.

    The intended usage is to return this from `find_matches()` so the resolver
    can iterate through the sequence multiple times, but only access the index