Skip to content

Commit 8ff3c6d

Browse files
committedJan 5, 2023
mypy: Iterator is better than Generator
1 parent 4ea850a commit 8ff3c6d

10 files changed

+26
-27
lines changed
 

‎coverage/bytecode.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from __future__ import annotations
77

88
from types import CodeType
9-
from typing import Generator
9+
from typing import Iterator
1010

1111

12-
def code_objects(code: CodeType) -> Generator[CodeType, None, None]:
12+
def code_objects(code: CodeType) -> Iterator[CodeType]:
1313
"""Iterate over all the code objects in `code`."""
1414
stack = [code]
1515
while stack:

‎coverage/control.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from types import FrameType
2121
from typing import (
2222
cast,
23-
Any, Callable, Dict, Generator, IO, Iterable, List, Optional, Tuple, Union,
23+
Any, Callable, Dict, IO, Iterable, Iterator, List, Optional, Tuple, Union,
2424
)
2525

2626
from coverage import env
@@ -56,7 +56,7 @@
5656
os = isolate_module(os)
5757

5858
@contextlib.contextmanager
59-
def override_config(cov: Coverage, **kwargs: TConfigValueIn) -> Generator[None, None, None]:
59+
def override_config(cov: Coverage, **kwargs: TConfigValueIn) -> Iterator[None]:
6060
"""Temporarily tweak the configuration of `cov`.
6161
6262
The arguments are applied to `cov.config` with the `from_args` method.

‎coverage/debug.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import _thread
1919

2020
from typing import (
21-
Any, Callable, Generator, IO, Iterable, Iterator, Optional, List, Tuple,
22-
cast,
21+
Any, Callable, IO, Iterable, Iterator, Optional, List, Tuple, cast,
2322
)
2423

2524
from coverage.misc import isolate_module
@@ -65,7 +64,7 @@ def should(self, option: str) -> bool:
6564
return (option in self.options)
6665

6766
@contextlib.contextmanager
68-
def without_callers(self) -> Generator[None, None, None]:
67+
def without_callers(self) -> Iterator[None]:
6968
"""A context manager to prevent call stacks from being logged."""
7069
old = self.suppress_callers
7170
self.suppress_callers = True

‎coverage/misc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from types import ModuleType
2323
from typing import (
24-
Any, Callable, Dict, Generator, IO, Iterable, List, Mapping, Optional,
24+
Any, Callable, Dict, IO, Iterable, Iterator, List, Mapping, Optional,
2525
Sequence, Tuple, TypeVar, Union,
2626
)
2727

@@ -71,7 +71,7 @@ def restore(self) -> None:
7171

7272

7373
@contextlib.contextmanager
74-
def sys_modules_saved() -> Generator[None, None, None]:
74+
def sys_modules_saved() -> Iterator[None]:
7575
"""A context manager to remove any modules imported during a block."""
7676
saver = SysModuleSaver()
7777
try:

‎coverage/sqldata.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import zlib
2323

2424
from typing import (
25-
cast, Any, Callable, Collection, Dict, Generator, Iterable, List, Mapping, Optional,
26-
Sequence, Set, Tuple, TypeVar, Union,
25+
cast, Any, Callable, Collection, Dict, Iterable, Iterator, List, Mapping,
26+
Optional, Sequence, Set, Tuple, TypeVar, Union,
2727
)
2828

2929
from coverage.debug import NoDebugging, AutoReprMixin, clipped_repr
@@ -1171,7 +1171,7 @@ def execute(
11711171
self,
11721172
sql: str,
11731173
parameters: Iterable[Any]=(),
1174-
) -> Generator[sqlite3.Cursor, None, None]:
1174+
) -> Iterator[sqlite3.Cursor]:
11751175
"""Context managed :meth:`python:sqlite3.Connection.execute`.
11761176
11771177
Use with a ``with`` statement to auto-close the returned cursor.

‎tests/conftest.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import warnings
1616

1717
from pathlib import Path
18-
from typing import Generator, Optional
18+
from typing import Iterator, Optional
1919

2020
import pytest
2121

@@ -65,15 +65,15 @@ def set_warnings() -> None:
6565

6666

6767
@pytest.fixture(autouse=True)
68-
def reset_sys_path() -> Generator[None, None, None]:
68+
def reset_sys_path() -> Iterator[None]:
6969
"""Clean up sys.path changes around every test."""
7070
sys_path = list(sys.path)
7171
yield
7272
sys.path[:] = sys_path
7373

7474

7575
@pytest.fixture(autouse=True)
76-
def reset_environment() -> Generator[None, None, None]:
76+
def reset_environment() -> Iterator[None]:
7777
"""Make sure a test setting an envvar doesn't leak into another test."""
7878
old_environ = os.environ.copy()
7979
yield
@@ -82,7 +82,7 @@ def reset_environment() -> Generator[None, None, None]:
8282

8383

8484
@pytest.fixture(autouse=True)
85-
def reset_filesdotpy_globals() -> Generator[None, None, None]:
85+
def reset_filesdotpy_globals() -> Iterator[None]:
8686
"""coverage/files.py has some unfortunate globals. Reset them every test."""
8787
set_relative_directory()
8888
yield
@@ -110,7 +110,7 @@ def pytest_sessionfinish() -> None:
110110
pth_file.unlink()
111111

112112

113-
def possible_pth_dirs() -> Generator[Path, None, None]:
113+
def possible_pth_dirs() -> Iterator[Path]:
114114
"""Produce a sequence of directories for trying to write .pth files."""
115115
# First look through sys.path, and if we find a .pth file, then it's a good
116116
# place to put ours.

‎tests/coveragetest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from types import ModuleType
2121
from typing import (
22-
Any, Collection, Dict, Generator, Iterable, List, Mapping, Optional,
22+
Any, Collection, Dict, Iterable, Iterator, List, Mapping, Optional,
2323
Sequence, Tuple, Union,
2424
)
2525

@@ -272,7 +272,7 @@ def assert_warnings(
272272
cov: Coverage,
273273
warnings: Iterable[str],
274274
not_warnings: Iterable[str]=(),
275-
) -> Generator[None, None, None]:
275+
) -> Iterator[None]:
276276
"""A context manager to check that particular warnings happened in `cov`.
277277
278278
`cov` is a Coverage instance. `warnings` is a list of regexes. Every

‎tests/helpers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import warnings
1717

1818
from typing import (
19-
cast,
20-
Any, Callable, Generator, Iterable, List, Optional, Set, Tuple, Type, TypeVar, Union,
19+
Any, Callable, Iterable, Iterator, List, Optional, Set, Tuple, Type,
20+
TypeVar, Union, cast,
2121
)
2222

2323
import pytest
@@ -267,7 +267,7 @@ def arcs_to_arcz_repr(arcs: Optional[Iterable[TArc]]) -> str:
267267

268268

269269
@contextlib.contextmanager
270-
def change_dir(new_dir: str) -> Generator[None, None, None]:
270+
def change_dir(new_dir: str) -> Iterator[None]:
271271
"""Change directory, and then change back.
272272
273273
Use as a context manager, it will return to the original
@@ -322,7 +322,7 @@ def assert_coverage_warnings(
322322
def swallow_warnings(
323323
message: str=r".",
324324
category: Type[Warning]=CoverageWarning,
325-
) -> Generator[None, None, None]:
325+
) -> Iterator[None]:
326326
"""Swallow particular warnings.
327327
328328
It's OK if they happen, or if they don't happen. Just ignore them.

‎tests/test_execfile.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import re
1515
import sys
1616

17-
from typing import Any, Generator
17+
from typing import Any, Iterator
1818

1919
import pytest
2020

@@ -31,7 +31,7 @@ class RunFileTest(CoverageTest):
3131
"""Test cases for `run_python_file`."""
3232

3333
@pytest.fixture(autouse=True)
34-
def clean_up(self) -> Generator[None, None, None]:
34+
def clean_up(self) -> Iterator[None]:
3535
"""These tests all run in-process. Clean up global changes."""
3636
yield
3737
sys.excepthook = sys.__excepthook__

‎tests/test_files.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import os.path
1111
import re
1212

13-
from typing import Any, Generator, Iterable, List
13+
from typing import Any, Iterable, Iterator, List
1414
from unittest import mock
1515

1616
import pytest
@@ -137,7 +137,7 @@ def globs_to_regex_params(
137137
partial: bool=False,
138138
matches: Iterable[str]=(),
139139
nomatches: Iterable[str]=(),
140-
) -> Generator[Any, None, None]:
140+
) -> Iterator[Any]:
141141
"""Generate parameters for `test_globs_to_regex`.
142142
143143
`patterns`, `case_insensitive`, and `partial` are arguments for

0 commit comments

Comments
 (0)
Please sign in to comment.