Skip to content

Commit 6f8ab49

Browse files
authored
TYP: contextmanager expects a Generator (#48383)
1 parent d1d9b7f commit 6f8ab49

File tree

16 files changed

+42
-32
lines changed

16 files changed

+42
-32
lines changed

pandas/_config/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@
5959
from typing import (
6060
Any,
6161
Callable,
62+
Generator,
6263
Generic,
6364
Iterable,
64-
Iterator,
6565
NamedTuple,
6666
cast,
6767
)
@@ -743,7 +743,7 @@ def pp(name: str, ks: Iterable[str]) -> list[str]:
743743

744744

745745
@contextmanager
746-
def config_prefix(prefix) -> Iterator[None]:
746+
def config_prefix(prefix) -> Generator[None, None, None]:
747747
"""
748748
contextmanager for multiple invocations of API with a common prefix
749749

pandas/_config/localization.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import subprocess
1212
from typing import (
1313
Callable,
14-
Iterator,
14+
Generator,
1515
)
1616

1717
from pandas._config.config import options
@@ -20,7 +20,7 @@
2020
@contextmanager
2121
def set_locale(
2222
new_locale: str | tuple[str, str], lc_var: int = locale.LC_ALL
23-
) -> Iterator[str | tuple[str, str]]:
23+
) -> Generator[str | tuple[str, str], None, None]:
2424
"""
2525
Context manager for temporarily setting a locale.
2626

pandas/_testing/_warnings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import sys
99
from typing import (
10+
Generator,
1011
Literal,
1112
Sequence,
1213
Type,
@@ -24,7 +25,7 @@ def assert_produces_warning(
2425
check_stacklevel: bool = True,
2526
raise_on_extra_warnings: bool = True,
2627
match: str | None = None,
27-
):
28+
) -> Generator[list[warnings.WarningMessage], None, None]:
2829
"""
2930
Context manager for running code expected to either raise a specific warning,
3031
multiple specific warnings, or not raise any warnings. Verifies that the code

pandas/_testing/contexts.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import (
99
IO,
1010
Any,
11-
Iterator,
11+
Generator,
1212
)
1313
import uuid
1414

@@ -20,7 +20,7 @@
2020

2121

2222
@contextmanager
23-
def decompress_file(path, compression) -> Iterator[IO[bytes]]:
23+
def decompress_file(path, compression) -> Generator[IO[bytes], None, None]:
2424
"""
2525
Open a compressed file and return a file object.
2626
@@ -41,7 +41,7 @@ def decompress_file(path, compression) -> Iterator[IO[bytes]]:
4141

4242

4343
@contextmanager
44-
def set_timezone(tz: str) -> Iterator[None]:
44+
def set_timezone(tz: str) -> Generator[None, None, None]:
4545
"""
4646
Context manager for temporarily setting a timezone.
4747
@@ -84,7 +84,9 @@ def setTZ(tz):
8484

8585

8686
@contextmanager
87-
def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any):
87+
def ensure_clean(
88+
filename=None, return_filelike: bool = False, **kwargs: Any
89+
) -> Generator[Any, None, None]:
8890
"""
8991
Gets a temporary path and agrees to remove on close.
9092
@@ -127,7 +129,7 @@ def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any):
127129

128130

129131
@contextmanager
130-
def ensure_clean_dir() -> Iterator[str]:
132+
def ensure_clean_dir() -> Generator[str, None, None]:
131133
"""
132134
Get a temporary directory path and agrees to remove on close.
133135
@@ -146,7 +148,7 @@ def ensure_clean_dir() -> Iterator[str]:
146148

147149

148150
@contextmanager
149-
def ensure_safe_environment_variables() -> Iterator[None]:
151+
def ensure_safe_environment_variables() -> Generator[None, None, None]:
150152
"""
151153
Get a context manager to safely set environment variables
152154
@@ -162,7 +164,7 @@ def ensure_safe_environment_variables() -> Iterator[None]:
162164

163165

164166
@contextmanager
165-
def with_csv_dialect(name, **kwargs) -> Iterator[None]:
167+
def with_csv_dialect(name, **kwargs) -> Generator[None, None, None]:
166168
"""
167169
Context manager to temporarily register a CSV dialect for parsing CSV.
168170
@@ -196,7 +198,7 @@ def with_csv_dialect(name, **kwargs) -> Iterator[None]:
196198

197199

198200
@contextmanager
199-
def use_numexpr(use, min_elements=None) -> Iterator[None]:
201+
def use_numexpr(use, min_elements=None) -> Generator[None, None, None]:
200202
from pandas.core.computation import expressions as expr
201203

202204
if min_elements is None:

pandas/compat/pickle_compat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pickle as pkl
1010
from typing import (
1111
TYPE_CHECKING,
12-
Iterator,
12+
Generator,
1313
)
1414
import warnings
1515

@@ -294,7 +294,7 @@ def loads(
294294

295295

296296
@contextlib.contextmanager
297-
def patch_pickle() -> Iterator[None]:
297+
def patch_pickle() -> Generator[None, None, None]:
298298
"""
299299
Temporarily patch pickle to use our unpickler.
300300
"""

pandas/core/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
Any,
1919
Callable,
2020
Collection,
21+
Generator,
2122
Hashable,
2223
Iterable,
23-
Iterator,
2424
Sequence,
2525
cast,
2626
overload,
@@ -534,7 +534,7 @@ def convert_to_list_like(
534534

535535

536536
@contextlib.contextmanager
537-
def temp_setattr(obj, attr: str, value) -> Iterator[None]:
537+
def temp_setattr(obj, attr: str, value) -> Generator[None, None, None]:
538538
"""Temporarily set attribute on an object.
539539
540540
Args:

pandas/core/groupby/groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class providing the base-class of operations.
2020
from typing import (
2121
TYPE_CHECKING,
2222
Callable,
23+
Generator,
2324
Hashable,
2425
Iterable,
2526
Iterator,
@@ -1106,7 +1107,7 @@ def _reset_group_selection(self) -> None:
11061107
self._reset_cache("_selected_obj")
11071108

11081109
@contextmanager
1109-
def _group_selection_context(self) -> Iterator[GroupBy]:
1110+
def _group_selection_context(self) -> Generator[GroupBy, None, None]:
11101111
"""
11111112
Set / reset the _group_selection_context.
11121113
"""

pandas/io/formats/format.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
Any,
2222
Callable,
2323
Final,
24+
Generator,
2425
Hashable,
2526
Iterable,
26-
Iterator,
2727
List,
2828
Mapping,
2929
Sequence,
@@ -1216,7 +1216,7 @@ def save_to_buffer(
12161216
@contextmanager
12171217
def get_buffer(
12181218
buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None
1219-
) -> Iterator[WriteBuffer[str]] | Iterator[StringIO]:
1219+
) -> Generator[WriteBuffer[str], None, None] | Generator[StringIO, None, None]:
12201220
"""
12211221
Context manager to open, yield and close buffer for filenames or Path-like
12221222
objects, otherwise yield buf unchanged.

pandas/io/formats/style.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import (
1212
Any,
1313
Callable,
14+
Generator,
1415
Hashable,
1516
Sequence,
1617
overload,
@@ -80,7 +81,7 @@
8081

8182

8283
@contextmanager
83-
def _mpl(func: Callable):
84+
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
8485
if has_mpl:
8586
yield plt, mpl
8687
else:

pandas/plotting/_matplotlib/converter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
TYPE_CHECKING,
1313
Any,
1414
Final,
15-
Iterator,
15+
Generator,
1616
cast,
1717
)
1818

@@ -99,7 +99,7 @@ def wrapper(*args, **kwargs):
9999

100100

101101
@contextlib.contextmanager
102-
def pandas_converters() -> Iterator[None]:
102+
def pandas_converters() -> Generator[None, None, None]:
103103
"""
104104
Context manager registering pandas' converters for a plot.
105105

pandas/plotting/_misc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from contextlib import contextmanager
44
from typing import (
55
TYPE_CHECKING,
6-
Iterator,
6+
Generator,
77
)
88

99
from pandas.plotting._core import _get_plot_backend
@@ -594,7 +594,7 @@ def _get_canonical_key(self, key):
594594
return self._ALIASES.get(key, key)
595595

596596
@contextmanager
597-
def use(self, key, value) -> Iterator[_Options]:
597+
def use(self, key, value) -> Generator[_Options, None, None]:
598598
"""
599599
Temporarily set a parameter value using the with statement.
600600
Aliasing allowed.

pandas/tests/io/pytables/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import contextmanager
22
import os
33
import tempfile
4+
from typing import Generator
45

56
import pytest
67

@@ -36,7 +37,9 @@ def create_tempfile(path):
3637

3738
# contextmanager to ensure the file cleanup
3839
@contextmanager
39-
def ensure_clean_store(path, mode="a", complevel=None, complib=None, fletcher32=False):
40+
def ensure_clean_store(
41+
path, mode="a", complevel=None, complib=None, fletcher32=False
42+
) -> Generator[HDFStore, None, None]:
4043

4144
try:
4245

pandas/tests/libs/test_hashtable.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import contextmanager
22
import struct
33
import tracemalloc
4+
from typing import Generator
45

56
import numpy as np
67
import pytest
@@ -13,7 +14,7 @@
1314

1415

1516
@contextmanager
16-
def activated_tracemalloc():
17+
def activated_tracemalloc() -> Generator[None, None, None]:
1718
tracemalloc.start()
1819
try:
1920
yield

pandas/tests/test_register_accessor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
from typing import Generator
23

34
import pytest
45

@@ -23,7 +24,7 @@ def __init__(self) -> None:
2324

2425

2526
@contextlib.contextmanager
26-
def ensure_removed(obj, attr):
27+
def ensure_removed(obj, attr) -> Generator[None, None, None]:
2728
"""Ensure that an attribute added to 'obj' during the test is
2829
removed when we're done
2930
"""

pandas/util/_exceptions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import functools
55
import inspect
66
import os
7-
from typing import Iterator
7+
from typing import Generator
88

99

1010
@contextlib.contextmanager
11-
def rewrite_exception(old_name: str, new_name: str) -> Iterator[None]:
11+
def rewrite_exception(old_name: str, new_name: str) -> Generator[None, None, None]:
1212
"""
1313
Rewrite the message of an exception.
1414
"""

pandas/util/_test_decorators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_foo():
3030
import locale
3131
from typing import (
3232
Callable,
33-
Iterator,
33+
Generator,
3434
)
3535
import warnings
3636

@@ -257,7 +257,7 @@ def check_file_leaks(func) -> Callable:
257257

258258

259259
@contextmanager
260-
def file_leak_context() -> Iterator[None]:
260+
def file_leak_context() -> Generator[None, None, None]:
261261
"""
262262
ContextManager analogue to check_file_leaks.
263263
"""

0 commit comments

Comments
 (0)