Skip to content

Replace Iterator[T] with Generator[T,None, None] #11007

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

Merged
merged 3 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions news/20ef81f6-dfa6-453a-a23a-74b0de4c2f88.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace ``Iterator[T]`` with ``Generator[T, None, None]``.
4 changes: 2 additions & 2 deletions src/pip/_internal/build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from collections import OrderedDict
from sysconfig import get_paths
from types import TracebackType
from typing import TYPE_CHECKING, Iterable, Iterator, List, Optional, Set, Tuple, Type
from typing import TYPE_CHECKING, Generator, Iterable, List, Optional, Set, Tuple, Type

from pip._vendor.certifi import where
from pip._vendor.packaging.requirements import Requirement
Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self, path: str) -> None:


@contextlib.contextmanager
def _create_standalone_pip() -> Iterator[str]:
def _create_standalone_pip() -> Generator[str, None, None]:
"""Create a "standalone pip" zip file.

The zip file's content is identical to the currently-running pip.
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/cli/command_context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextlib import ExitStack, contextmanager
from typing import ContextManager, Iterator, TypeVar
from typing import ContextManager, Generator, TypeVar

_T = TypeVar("_T", covariant=True)

Expand All @@ -11,7 +11,7 @@ def __init__(self) -> None:
self._main_context = ExitStack()

@contextmanager
def main_context(self) -> Iterator[None]:
def main_context(self) -> Generator[None, None, None]:
assert not self._in_main_context

self._in_main_context = True
Expand Down
6 changes: 4 additions & 2 deletions src/pip/_internal/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import textwrap
from contextlib import suppress
from typing import Any, Dict, Iterator, List, Tuple
from typing import Any, Dict, Generator, List, Tuple

from pip._internal.cli.status_codes import UNKNOWN_ERROR
from pip._internal.configuration import Configuration, ConfigurationError
Expand Down Expand Up @@ -175,7 +175,9 @@ def check_default(self, option: optparse.Option, key: str, val: Any) -> Any:
print(f"An error occurred during configuration: {exc}")
sys.exit(3)

def _get_ordered_configuration_items(self) -> Iterator[Tuple[str, Any]]:
def _get_ordered_configuration_items(
self,
) -> Generator[Tuple[str, Any], None, None]:
# Configuration gives keys in an unordered manner. Order them.
override_order = ["global", self.name, ":env:"]

Expand Down
8 changes: 4 additions & 4 deletions src/pip/_internal/cli/progress_bars.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import functools
from typing import Callable, Iterator, Optional, Tuple
from typing import Callable, Generator, Iterable, Iterator, Optional, Tuple

from pip._vendor.rich.progress import (
BarColumn,
Expand All @@ -16,15 +16,15 @@

from pip._internal.utils.logging import get_indentation

DownloadProgressRenderer = Callable[[Iterator[bytes]], Iterator[bytes]]
DownloadProgressRenderer = Callable[[Iterable[bytes]], Iterator[bytes]]


def _rich_progress_bar(
iterable: Iterator[bytes],
iterable: Iterable[bytes],
*,
bar_type: str,
size: int,
) -> Iterator[bytes]:
) -> Generator[bytes, None, None]:
assert bar_type == "on", "This should only be used in the default mode."

if not size:
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/cli/spinners.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import sys
import time
from typing import IO, Iterator
from typing import IO, Generator

from pip._internal.utils.compat import WINDOWS
from pip._internal.utils.logging import get_indentation
Expand Down Expand Up @@ -113,7 +113,7 @@ def reset(self) -> None:


@contextlib.contextmanager
def open_spinner(message: str) -> Iterator[SpinnerInterface]:
def open_spinner(message: str) -> Generator[SpinnerInterface, None, None]:
# Interactive spinner goes directly to sys.stdout rather than being routed
# through the logging system, but it acts like it has level INFO,
# i.e. it's only displayed if we're at level INFO or better.
Expand Down Expand Up @@ -141,7 +141,7 @@ def open_spinner(message: str) -> Iterator[SpinnerInterface]:


@contextlib.contextmanager
def hidden_cursor(file: IO[str]) -> Iterator[None]:
def hidden_cursor(file: IO[str]) -> Generator[None, None, None]:
# The Windows terminal does not support the hide/show cursor ANSI codes,
# even via colorama. So don't even try.
if WINDOWS:
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
from optparse import Values
from typing import TYPE_CHECKING, Iterator, List, Optional, Sequence, Tuple, cast
from typing import TYPE_CHECKING, Generator, List, Optional, Sequence, Tuple, cast

from pip._vendor.packaging.utils import canonicalize_name

Expand Down Expand Up @@ -222,7 +222,7 @@ def get_not_required(

def iter_packages_latest_infos(
self, packages: "_ProcessedDists", options: Values
) -> Iterator["_DistWithLatestInfo"]:
) -> Generator["_DistWithLatestInfo", None, None]:
with self._build_session(options) as session:
finder = self._build_package_finder(options, session)

Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/commands/show.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from optparse import Values
from typing import Iterator, List, NamedTuple, Optional
from typing import Generator, Iterable, Iterator, List, NamedTuple, Optional

from pip._vendor.packaging.utils import canonicalize_name

Expand Down Expand Up @@ -67,7 +67,7 @@ class _PackageInfo(NamedTuple):
files: Optional[List[str]]


def search_packages_info(query: List[str]) -> Iterator[_PackageInfo]:
def search_packages_info(query: List[str]) -> Generator[_PackageInfo, None, None]:
"""
Gather details from installed distributions. Print distribution name,
version, location, and installed files. Installed files requires a
Expand Down Expand Up @@ -135,7 +135,7 @@ def _get_requiring_packages(current_dist: BaseDistribution) -> Iterator[str]:


def print_results(
distributions: Iterator[_PackageInfo],
distributions: Iterable[_PackageInfo],
list_files: bool,
verbose: bool,
) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/locations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
import sys
import sysconfig
from typing import Any, Dict, Iterator, List, Optional, Tuple
from typing import Any, Dict, Generator, List, Optional, Tuple

from pip._internal.models.scheme import SCHEME_KEYS, Scheme
from pip._internal.utils.compat import WINDOWS
Expand Down Expand Up @@ -169,7 +169,7 @@ def _looks_like_msys2_mingw_scheme() -> bool:
)


def _fix_abiflags(parts: Tuple[str]) -> Iterator[str]:
def _fix_abiflags(parts: Tuple[str]) -> Generator[str, None, None]:
ldversion = sysconfig.get_config_var("LDVERSION")
abiflags = getattr(sys, "abiflags", None)

Expand Down
3 changes: 2 additions & 1 deletion src/pip/_internal/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
TYPE_CHECKING,
Collection,
Container,
Generator,
Iterable,
Iterator,
List,
Expand Down Expand Up @@ -470,7 +471,7 @@ def _iter_distributions(self) -> Iterator["BaseDistribution"]:
"""
raise NotImplementedError()

def iter_distributions(self) -> Iterator["BaseDistribution"]:
def iter_distributions(self) -> Generator["BaseDistribution", None, None]:
"""Iterate through installed distributions."""
for dist in self._iter_distributions():
# Make sure the distribution actually comes from a valid Python
Expand Down
6 changes: 3 additions & 3 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import pathlib
import zipfile
from typing import Collection, Iterable, Iterator, List, Mapping, NamedTuple, Optional
from typing import Collection, Generator, Iterable, List, Mapping, NamedTuple, Optional

from pip._vendor import pkg_resources
from pip._vendor.packaging.requirements import Requirement
Expand Down Expand Up @@ -149,7 +149,7 @@ def version(self) -> DistributionVersion:
def is_file(self, path: InfoPath) -> bool:
return self._dist.has_metadata(str(path))

def iterdir(self, path: InfoPath) -> Iterator[pathlib.PurePosixPath]:
def iterdir(self, path: InfoPath) -> Generator[pathlib.PurePosixPath, None, None]:
name = str(path)
if not self._dist.has_metadata(name):
raise FileNotFoundError(name)
Expand Down Expand Up @@ -251,6 +251,6 @@ def get_distribution(self, name: str) -> Optional[BaseDistribution]:
return None
return self._search_distribution(name)

def _iter_distributions(self) -> Iterator[BaseDistribution]:
def _iter_distributions(self) -> Generator[BaseDistribution, None, None]:
for dist in self._ws:
yield Distribution(dist)
4 changes: 2 additions & 2 deletions src/pip/_internal/network/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import os
from contextlib import contextmanager
from typing import Iterator, Optional
from typing import Generator, Optional

from pip._vendor.cachecontrol.cache import BaseCache
from pip._vendor.cachecontrol.caches import FileCache
Expand All @@ -18,7 +18,7 @@ def is_from_cache(response: Response) -> bool:


@contextmanager
def suppressed_cache_errors() -> Iterator[None]:
def suppressed_cache_errors() -> Generator[None, None, None]:
"""If we can't access the cache then we can just skip caching and process
requests as if caching wasn't enabled.
"""
Expand Down
8 changes: 4 additions & 4 deletions src/pip/_internal/network/lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bisect import bisect_left, bisect_right
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
from typing import Any, Dict, Iterator, List, Optional, Tuple
from typing import Any, Dict, Generator, List, Optional, Tuple
from zipfile import BadZipfile, ZipFile

from pip._vendor.packaging.utils import canonicalize_name
Expand Down Expand Up @@ -139,7 +139,7 @@ def __exit__(self, *exc: Any) -> Optional[bool]:
return self._file.__exit__(*exc)

@contextmanager
def _stay(self) -> Iterator[None]:
def _stay(self) -> Generator[None, None, None]:
"""Return a context manager keeping the position.

At the end of the block, seek back to original position.
Expand Down Expand Up @@ -177,8 +177,8 @@ def _stream_response(

def _merge(
self, start: int, end: int, left: int, right: int
) -> Iterator[Tuple[int, int]]:
"""Return an iterator of intervals to be fetched.
) -> Generator[Tuple[int, int], None, None]:
"""Return a generator of intervals to be fetched.

Args:
start (int): Start of needed interval
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import sys
import urllib.parse
import warnings
from typing import Any, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple, Union
from typing import Any, Dict, Generator, List, Mapping, Optional, Sequence, Tuple, Union

from pip._vendor import requests, urllib3
from pip._vendor.cachecontrol import CacheControlAdapter
Expand Down Expand Up @@ -374,7 +374,7 @@ def add_trusted_host(
# Mount wildcard ports for the same host.
self.mount(build_url_from_netloc(host) + ":", self._trusted_host_adapter)

def iter_secure_origins(self) -> Iterator[SecureOrigin]:
def iter_secure_origins(self) -> Generator[SecureOrigin, None, None]:
yield from SECURE_ORIGINS
for host, port in self.pip_trusted_origins:
yield ("*", host, "*" if port is None else port)
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/network/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Iterator
from typing import Dict, Generator

from pip._vendor.requests.models import CONTENT_CHUNK_SIZE, Response

Expand Down Expand Up @@ -56,7 +56,7 @@ def raise_for_status(resp: Response) -> None:

def response_chunks(
response: Response, chunk_size: int = CONTENT_CHUNK_SIZE
) -> Iterator[bytes]:
) -> Generator[bytes, None, None]:
"""Given a requests Response, provide the data chunks."""
try:
# Special case for urllib3.
Expand Down
8 changes: 4 additions & 4 deletions src/pip/_internal/operations/build/build_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
from types import TracebackType
from typing import Dict, Iterator, Optional, Set, Type, Union
from typing import Dict, Generator, Optional, Set, Type, Union

from pip._internal.models.link import Link
from pip._internal.req.req_install import InstallRequirement
Expand All @@ -13,7 +13,7 @@


@contextlib.contextmanager
def update_env_context_manager(**changes: str) -> Iterator[None]:
def update_env_context_manager(**changes: str) -> Generator[None, None, None]:
target = os.environ

# Save values from the target and change them.
Expand All @@ -39,7 +39,7 @@ def update_env_context_manager(**changes: str) -> Iterator[None]:


@contextlib.contextmanager
def get_build_tracker() -> Iterator["BuildTracker"]:
def get_build_tracker() -> Generator["BuildTracker", None, None]:
root = os.environ.get("PIP_BUILD_TRACKER")
with contextlib.ExitStack() as ctx:
if root is None:
Expand Down Expand Up @@ -118,7 +118,7 @@ def cleanup(self) -> None:
logger.debug("Removed build tracker: %r", self._root)

@contextlib.contextmanager
def track(self, req: InstallRequirement) -> Iterator[None]:
def track(self, req: InstallRequirement) -> Generator[None, None, None]:
self.add(req)
yield
self.remove(req)
4 changes: 2 additions & 2 deletions src/pip/_internal/operations/freeze.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collections
import logging
import os
from typing import Container, Dict, Iterable, Iterator, List, NamedTuple, Optional, Set
from typing import Container, Dict, Generator, Iterable, List, NamedTuple, Optional, Set

from pip._vendor.packaging.utils import canonicalize_name
from pip._vendor.packaging.version import Version
Expand Down Expand Up @@ -31,7 +31,7 @@ def freeze(
isolated: bool = False,
exclude_editable: bool = False,
skip: Container[str] = (),
) -> Iterator[str]:
) -> Generator[str, None, None]:
installations: Dict[str, FrozenRequirement] = {}

dists = get_environment(paths).iter_installed_distributions(
Expand Down
7 changes: 4 additions & 3 deletions src/pip/_internal/operations/install/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
BinaryIO,
Callable,
Dict,
Generator,
Iterable,
Iterator,
List,
Expand Down Expand Up @@ -589,7 +590,7 @@ def is_entrypoint_wrapper(file: "File") -> bool:
file.save()
record_installed(file.src_record_path, file.dest_path, file.changed)

def pyc_source_file_paths() -> Iterator[str]:
def pyc_source_file_paths() -> Generator[str, None, None]:
# We de-duplicate installation paths, since there can be overlap (e.g.
# file in .data maps to same location as file in wheel root).
# Sorting installation paths makes it easier to reproduce and debug
Expand Down Expand Up @@ -656,7 +657,7 @@ def pyc_output_path(path: str) -> str:
generated_file_mode = 0o666 & ~current_umask()

@contextlib.contextmanager
def _generate_file(path: str, **kwargs: Any) -> Iterator[BinaryIO]:
def _generate_file(path: str, **kwargs: Any) -> Generator[BinaryIO, None, None]:
with adjacent_tmp_file(path, **kwargs) as f:
yield f
os.chmod(f.name, generated_file_mode)
Expand Down Expand Up @@ -706,7 +707,7 @@ def _generate_file(path: str, **kwargs: Any) -> Iterator[BinaryIO]:


@contextlib.contextmanager
def req_error_context(req_description: str) -> Iterator[None]:
def req_error_context(req_description: str) -> Generator[None, None, None]:
try:
yield
except InstallationError as e:
Expand Down
4 changes: 2 additions & 2 deletions src/pip/_internal/req/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import collections
import logging
from typing import Iterator, List, Optional, Sequence, Tuple
from typing import Generator, List, Optional, Sequence, Tuple

from pip._internal.utils.logging import indent_log

Expand Down Expand Up @@ -28,7 +28,7 @@ def __repr__(self) -> str:

def _validate_requirements(
requirements: List[InstallRequirement],
) -> Iterator[Tuple[str, InstallRequirement]]:
) -> Generator[Tuple[str, InstallRequirement], None, None]:
for req in requirements:
assert req.name, f"invalid to-be-installed requirement: {req}"
yield req.name, req
Expand Down
Loading