Skip to content

TYP: contextmanager expects a Generator #48383

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 1 commit into from
Sep 6, 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
4 changes: 2 additions & 2 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
from typing import (
Any,
Callable,
Generator,
Generic,
Iterable,
Iterator,
NamedTuple,
cast,
)
Expand Down Expand Up @@ -743,7 +743,7 @@ def pp(name: str, ks: Iterable[str]) -> list[str]:


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

Expand Down
4 changes: 2 additions & 2 deletions pandas/_config/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import subprocess
from typing import (
Callable,
Iterator,
Generator,
)

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

Expand Down
3 changes: 2 additions & 1 deletion pandas/_testing/_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import sys
from typing import (
Generator,
Literal,
Sequence,
Type,
Expand All @@ -24,7 +25,7 @@ def assert_produces_warning(
check_stacklevel: bool = True,
raise_on_extra_warnings: bool = True,
match: str | None = None,
):
) -> Generator[list[warnings.WarningMessage], None, None]:
"""
Context manager for running code expected to either raise a specific warning,
multiple specific warnings, or not raise any warnings. Verifies that the code
Expand Down
18 changes: 10 additions & 8 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import (
IO,
Any,
Iterator,
Generator,
)
import uuid

Expand All @@ -20,7 +20,7 @@


@contextmanager
def decompress_file(path, compression) -> Iterator[IO[bytes]]:
def decompress_file(path, compression) -> Generator[IO[bytes], None, None]:
"""
Open a compressed file and return a file object.

Expand All @@ -41,7 +41,7 @@ def decompress_file(path, compression) -> Iterator[IO[bytes]]:


@contextmanager
def set_timezone(tz: str) -> Iterator[None]:
def set_timezone(tz: str) -> Generator[None, None, None]:
"""
Context manager for temporarily setting a timezone.

Expand Down Expand Up @@ -84,7 +84,9 @@ def setTZ(tz):


@contextmanager
def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any):
def ensure_clean(
filename=None, return_filelike: bool = False, **kwargs: Any
) -> Generator[Any, None, None]:
"""
Gets a temporary path and agrees to remove on close.

Expand Down Expand Up @@ -127,7 +129,7 @@ def ensure_clean(filename=None, return_filelike: bool = False, **kwargs: Any):


@contextmanager
def ensure_clean_dir() -> Iterator[str]:
def ensure_clean_dir() -> Generator[str, None, None]:
"""
Get a temporary directory path and agrees to remove on close.

Expand All @@ -146,7 +148,7 @@ def ensure_clean_dir() -> Iterator[str]:


@contextmanager
def ensure_safe_environment_variables() -> Iterator[None]:
def ensure_safe_environment_variables() -> Generator[None, None, None]:
"""
Get a context manager to safely set environment variables

Expand All @@ -162,7 +164,7 @@ def ensure_safe_environment_variables() -> Iterator[None]:


@contextmanager
def with_csv_dialect(name, **kwargs) -> Iterator[None]:
def with_csv_dialect(name, **kwargs) -> Generator[None, None, None]:
"""
Context manager to temporarily register a CSV dialect for parsing CSV.

Expand Down Expand Up @@ -196,7 +198,7 @@ def with_csv_dialect(name, **kwargs) -> Iterator[None]:


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

if min_elements is None:
Expand Down
4 changes: 2 additions & 2 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pickle as pkl
from typing import (
TYPE_CHECKING,
Iterator,
Generator,
)
import warnings

Expand Down Expand Up @@ -294,7 +294,7 @@ def loads(


@contextlib.contextmanager
def patch_pickle() -> Iterator[None]:
def patch_pickle() -> Generator[None, None, None]:
"""
Temporarily patch pickle to use our unpickler.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
Any,
Callable,
Collection,
Generator,
Hashable,
Iterable,
Iterator,
Sequence,
cast,
overload,
Expand Down Expand Up @@ -534,7 +534,7 @@ def convert_to_list_like(


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

Args:
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class providing the base-class of operations.
from typing import (
TYPE_CHECKING,
Callable,
Generator,
Hashable,
Iterable,
Iterator,
Expand Down Expand Up @@ -1106,7 +1107,7 @@ def _reset_group_selection(self) -> None:
self._reset_cache("_selected_obj")

@contextmanager
def _group_selection_context(self) -> Iterator[GroupBy]:
def _group_selection_context(self) -> Generator[GroupBy, None, None]:
"""
Set / reset the _group_selection_context.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
Any,
Callable,
Final,
Generator,
Hashable,
Iterable,
Iterator,
List,
Mapping,
Sequence,
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def save_to_buffer(
@contextmanager
def get_buffer(
buf: FilePath | WriteBuffer[str] | None, encoding: str | None = None
) -> Iterator[WriteBuffer[str]] | Iterator[StringIO]:
) -> Generator[WriteBuffer[str], None, None] | Generator[StringIO, None, None]:
"""
Context manager to open, yield and close buffer for filenames or Path-like
objects, otherwise yield buf unchanged.
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import (
Any,
Callable,
Generator,
Hashable,
Sequence,
overload,
Expand Down Expand Up @@ -80,7 +81,7 @@


@contextmanager
def _mpl(func: Callable):
def _mpl(func: Callable) -> Generator[tuple[Any, Any], None, None]:
if has_mpl:
yield plt, mpl
else:
Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_matplotlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
TYPE_CHECKING,
Any,
Final,
Iterator,
Generator,
cast,
)

Expand Down Expand Up @@ -99,7 +99,7 @@ def wrapper(*args, **kwargs):


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

Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from contextlib import contextmanager
from typing import (
TYPE_CHECKING,
Iterator,
Generator,
)

from pandas.plotting._core import _get_plot_backend
Expand Down Expand Up @@ -594,7 +594,7 @@ def _get_canonical_key(self, key):
return self._ALIASES.get(key, key)

@contextmanager
def use(self, key, value) -> Iterator[_Options]:
def use(self, key, value) -> Generator[_Options, None, None]:
"""
Temporarily set a parameter value using the with statement.
Aliasing allowed.
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/io/pytables/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import contextmanager
import os
import tempfile
from typing import Generator

import pytest

Expand Down Expand Up @@ -36,7 +37,9 @@ def create_tempfile(path):

# contextmanager to ensure the file cleanup
@contextmanager
def ensure_clean_store(path, mode="a", complevel=None, complib=None, fletcher32=False):
def ensure_clean_store(
path, mode="a", complevel=None, complib=None, fletcher32=False
) -> Generator[HDFStore, None, None]:

try:

Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/libs/test_hashtable.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import contextmanager
import struct
import tracemalloc
from typing import Generator

import numpy as np
import pytest
Expand All @@ -13,7 +14,7 @@


@contextmanager
def activated_tracemalloc():
def activated_tracemalloc() -> Generator[None, None, None]:
tracemalloc.start()
try:
yield
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/test_register_accessor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
from typing import Generator

import pytest

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


@contextlib.contextmanager
def ensure_removed(obj, attr):
def ensure_removed(obj, attr) -> Generator[None, None, None]:
"""Ensure that an attribute added to 'obj' during the test is
removed when we're done
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/util/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import functools
import inspect
import os
from typing import Iterator
from typing import Generator


@contextlib.contextmanager
def rewrite_exception(old_name: str, new_name: str) -> Iterator[None]:
def rewrite_exception(old_name: str, new_name: str) -> Generator[None, None, None]:
"""
Rewrite the message of an exception.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_foo():
import locale
from typing import (
Callable,
Iterator,
Generator,
)
import warnings

Expand Down Expand Up @@ -257,7 +257,7 @@ def check_file_leaks(func) -> Callable:


@contextmanager
def file_leak_context() -> Iterator[None]:
def file_leak_context() -> Generator[None, None, None]:
"""
ContextManager analogue to check_file_leaks.
"""
Expand Down