Skip to content

Commit 3820b0e

Browse files
q0wuranusjr
andauthored
Replace Iterator[T] with Generator[T,None, None] (#11007)
Co-authored-by: Tzu-ping Chung <[email protected]>
1 parent e679df4 commit 3820b0e

26 files changed

+84
-68
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace ``Iterator[T]`` with ``Generator[T, None, None]``.

src/pip/_internal/build_env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections import OrderedDict
1212
from sysconfig import get_paths
1313
from types import TracebackType
14-
from typing import TYPE_CHECKING, Iterable, Iterator, List, Optional, Set, Tuple, Type
14+
from typing import TYPE_CHECKING, Generator, Iterable, List, Optional, Set, Tuple, Type
1515

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

4343

4444
@contextlib.contextmanager
45-
def _create_standalone_pip() -> Iterator[str]:
45+
def _create_standalone_pip() -> Generator[str, None, None]:
4646
"""Create a "standalone pip" zip file.
4747
4848
The zip file's content is identical to the currently-running pip.

src/pip/_internal/cli/command_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from contextlib import ExitStack, contextmanager
2-
from typing import ContextManager, Iterator, TypeVar
2+
from typing import ContextManager, Generator, TypeVar
33

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

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

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

1717
self._in_main_context = True

src/pip/_internal/cli/parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import textwrap
88
from contextlib import suppress
9-
from typing import Any, Dict, Iterator, List, Tuple
9+
from typing import Any, Dict, Generator, List, Tuple
1010

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

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

src/pip/_internal/cli/progress_bars.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
from typing import Callable, Iterator, Optional, Tuple
2+
from typing import Callable, Generator, Iterable, Iterator, Optional, Tuple
33

44
from pip._vendor.rich.progress import (
55
BarColumn,
@@ -16,15 +16,15 @@
1616

1717
from pip._internal.utils.logging import get_indentation
1818

19-
DownloadProgressRenderer = Callable[[Iterator[bytes]], Iterator[bytes]]
19+
DownloadProgressRenderer = Callable[[Iterable[bytes]], Iterator[bytes]]
2020

2121

2222
def _rich_progress_bar(
23-
iterable: Iterator[bytes],
23+
iterable: Iterable[bytes],
2424
*,
2525
bar_type: str,
2626
size: int,
27-
) -> Iterator[bytes]:
27+
) -> Generator[bytes, None, None]:
2828
assert bar_type == "on", "This should only be used in the default mode."
2929

3030
if not size:

src/pip/_internal/cli/spinners.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import sys
55
import time
6-
from typing import IO, Iterator
6+
from typing import IO, Generator
77

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

114114

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

142142

143143
@contextlib.contextmanager
144-
def hidden_cursor(file: IO[str]) -> Iterator[None]:
144+
def hidden_cursor(file: IO[str]) -> Generator[None, None, None]:
145145
# The Windows terminal does not support the hide/show cursor ANSI codes,
146146
# even via colorama. So don't even try.
147147
if WINDOWS:

src/pip/_internal/commands/list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import logging
33
from optparse import Values
4-
from typing import TYPE_CHECKING, Iterator, List, Optional, Sequence, Tuple, cast
4+
from typing import TYPE_CHECKING, Generator, List, Optional, Sequence, Tuple, cast
55

66
from pip._vendor.packaging.utils import canonicalize_name
77

@@ -222,7 +222,7 @@ def get_not_required(
222222

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

src/pip/_internal/commands/show.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from optparse import Values
3-
from typing import Iterator, List, NamedTuple, Optional
3+
from typing import Generator, Iterable, Iterator, List, NamedTuple, Optional
44

55
from pip._vendor.packaging.utils import canonicalize_name
66

@@ -67,7 +67,7 @@ class _PackageInfo(NamedTuple):
6767
files: Optional[List[str]]
6868

6969

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

136136

137137
def print_results(
138-
distributions: Iterator[_PackageInfo],
138+
distributions: Iterable[_PackageInfo],
139139
list_files: bool,
140140
verbose: bool,
141141
) -> bool:

src/pip/_internal/locations/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pathlib
55
import sys
66
import sysconfig
7-
from typing import Any, Dict, Iterator, List, Optional, Tuple
7+
from typing import Any, Dict, Generator, List, Optional, Tuple
88

99
from pip._internal.models.scheme import SCHEME_KEYS, Scheme
1010
from pip._internal.utils.compat import WINDOWS
@@ -169,7 +169,7 @@ def _looks_like_msys2_mingw_scheme() -> bool:
169169
)
170170

171171

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

src/pip/_internal/metadata/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TYPE_CHECKING,
1111
Collection,
1212
Container,
13+
Generator,
1314
Iterable,
1415
Iterator,
1516
List,
@@ -470,7 +471,7 @@ def _iter_distributions(self) -> Iterator["BaseDistribution"]:
470471
"""
471472
raise NotImplementedError()
472473

473-
def iter_distributions(self) -> Iterator["BaseDistribution"]:
474+
def iter_distributions(self) -> Generator["BaseDistribution", None, None]:
474475
"""Iterate through installed distributions."""
475476
for dist in self._iter_distributions():
476477
# Make sure the distribution actually comes from a valid Python

0 commit comments

Comments
 (0)