Skip to content

stdlib: Improve a bunch of __(a)exit__ methods #7571

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 23 commits into from
Apr 1, 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
3 changes: 2 additions & 1 deletion stdlib/_dummy_thread.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from types import TracebackType
from typing import Any, Callable, NoReturn

TIMEOUT_MAX: int
Expand All @@ -14,7 +15,7 @@ class LockType:
def __init__(self) -> None: ...
def acquire(self, waitflag: bool | None = ..., timeout: int = ...) -> bool: ...
def __enter__(self, waitflag: bool | None = ..., timeout: int = ...) -> bool: ...
def __exit__(self, typ: Any, val: Any, tb: Any) -> None: ...
def __exit__(self, typ: type[BaseException] | None, val: BaseException | None, tb: TracebackType | None) -> None: ...
def release(self) -> bool: ...
def locked(self) -> bool: ...

Expand Down
2 changes: 1 addition & 1 deletion stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AbstractServer:
def close(self) -> None: ...
if sys.version_info >= (3, 7):
async def __aenter__(self: Self) -> Self: ...
async def __aexit__(self, *exc: Any) -> None: ...
async def __aexit__(self, *exc: object) -> None: ...
@abstractmethod
def get_loop(self) -> AbstractEventLoop: ...
@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion stdlib/asyncio/locks.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else:
class _ContextManager:
def __init__(self, lock: Lock | Semaphore) -> None: ...
def __enter__(self) -> None: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...

class _ContextManagerMixin:
# Apparently this exists to *prohibit* use as a context manager.
Expand Down
2 changes: 1 addition & 1 deletion stdlib/asyncio/windows_utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if sys.platform == "win32":
def __del__(self) -> None: ...

def __enter__(self: Self) -> Self: ...
def __exit__(self, t: type | None, v: BaseException | None, tb: TracebackType | None) -> None: ...
def __exit__(self, t: type[BaseException] | None, v: BaseException | None, tb: TracebackType | None) -> None: ...
@property
def handle(self) -> int: ...
def fileno(self) -> int: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/cProfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class Profile:
def runcall(self, __func: Callable[_P, _T], *args: _P.args, **kw: _P.kwargs) -> _T: ...
if sys.version_info >= (3, 8):
def __enter__(self: Self) -> Self: ...
def __exit__(self, *exc_info: Any) -> None: ...
def __exit__(self, *exc_info: object) -> None: ...

def label(code: str | CodeType) -> _Label: ... # undocumented
4 changes: 2 additions & 2 deletions stdlib/calendar.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import sys
from collections.abc import Iterable, Sequence
from time import struct_time
from typing import Any, Iterable, Sequence
from typing_extensions import Literal

__all__ = [
Expand Down Expand Up @@ -106,7 +106,7 @@ class HTMLCalendar(Calendar):
class different_locale:
def __init__(self, locale: _LocaleType) -> None: ...
def __enter__(self) -> None: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...

class LocaleTextCalendar(TextCalendar):
def __init__(self, firstweekday: int = ..., locale: _LocaleType | None = ...) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/cgi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class FieldStorage:
separator: str = ...,
) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
def __iter__(self) -> Iterator[str]: ...
def __getitem__(self, key: str) -> Any: ...
def getvalue(self, key: str, default: Any = ...) -> Any: ...
Expand Down
7 changes: 5 additions & 2 deletions stdlib/concurrent/futures/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from _typeshed import Self
from abc import abstractmethod
from collections.abc import Container, Iterable, Iterator, Sequence
from logging import Logger
from types import TracebackType
from typing import Any, Callable, Generic, Protocol, TypeVar, overload
from typing_extensions import Literal, ParamSpec, SupportsIndex

Expand Down Expand Up @@ -73,7 +74,9 @@ class Executor:
def shutdown(self, wait: bool = ...) -> None: ...

def __enter__(self: Self) -> Self: ...
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> bool | None: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
) -> bool | None: ...

def as_completed(fs: Iterable[Future[_T]], timeout: float | None = ...) -> Iterator[Future[_T]]: ...

Expand Down Expand Up @@ -127,4 +130,4 @@ class _AcquireFutures:
futures: Iterable[Future[Any]]
def __init__(self, futures: Iterable[Future[Any]]) -> None: ...
def __enter__(self) -> None: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
6 changes: 3 additions & 3 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ if sys.version_info >= (3, 10):
@overload
def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...
def __enter__(self) -> _T: ...
def __exit__(self, *exctype: Any) -> None: ...
def __exit__(self, *exctype: object) -> None: ...
async def __aenter__(self) -> _T: ...
async def __aexit__(self, *exctype: Any) -> None: ...
async def __aexit__(self, *exctype: object) -> None: ...

elif sys.version_info >= (3, 7):
class nullcontext(AbstractContextManager[_T]):
Expand All @@ -229,7 +229,7 @@ elif sys.version_info >= (3, 7):
@overload
def __init__(self: nullcontext[_T], enter_result: _T) -> None: ...
def __enter__(self) -> _T: ...
def __exit__(self, *exctype: Any) -> None: ...
def __exit__(self, *exctype: object) -> None: ...

if sys.version_info >= (3, 11):
_T_fd_or_any_path = TypeVar("_T_fd_or_any_path", bound=int | StrOrBytesPath)
Expand Down
5 changes: 4 additions & 1 deletion stdlib/fileinput.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from _typeshed import Self, StrOrBytesPath
from types import TracebackType
from typing import IO, Any, AnyStr, Callable, Generic, Iterable, Iterator

__all__ = [
Expand Down Expand Up @@ -98,7 +99,9 @@ class FileInput(Iterator[AnyStr], Generic[AnyStr]):
def __del__(self) -> None: ...
def close(self) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ...
def __exit__(
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> None: ...
def __iter__(self: Self) -> Self: ...
def __next__(self) -> AnyStr: ...
if sys.version_info < (3, 11):
Expand Down
5 changes: 4 additions & 1 deletion stdlib/multiprocessing/sharedctypes.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from collections.abc import Callable, Iterable, Sequence
from ctypes import _CData, _SimpleCData, c_char
from multiprocessing.context import BaseContext
from multiprocessing.synchronize import _LockLike
from types import TracebackType
from typing import Any, Generic, Protocol, TypeVar, overload
from typing_extensions import Literal

Expand Down Expand Up @@ -82,7 +83,9 @@ class SynchronizedBase(Generic[_CT]):
def get_obj(self) -> _CT: ...
def get_lock(self) -> _LockLike: ...
def __enter__(self) -> bool: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(
self, __exc_type: type[BaseException] | None, __exc_val: BaseException | None, __exc_tb: TracebackType | None
) -> None: ...

class Synchronized(SynchronizedBase[_SimpleCData[_T]], Generic[_T]):
value: _T
Expand Down
2 changes: 1 addition & 1 deletion stdlib/nntplib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class NNTP:
timeout: float = ...,
) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
def getwelcome(self) -> str: ...
def getcapabilities(self) -> dict[str, _list[str]]: ...
def set_debuglevel(self, level: int) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ if sys.version_info >= (3, 8):
def __init__(self, path: str | None, cookie: _T, remove_dll_directory: Callable[[_T], Any]) -> None: ...
def close(self) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...

def add_dll_directory(path: str) -> _AddedDllDirectory: ...
if sys.platform == "linux":
Expand Down
4 changes: 2 additions & 2 deletions stdlib/runpy.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class _TempModule:
module: ModuleType
def __init__(self, mod_name: str) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...

class _ModifiedArgv0:
value: Any
def __init__(self, value: Any) -> None: ...
def __enter__(self) -> None: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...

def run_module(
mod_name: str, init_globals: dict[str, Any] | None = ..., run_name: str | None = ..., alter_sys: bool = ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/selectors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BaseSelector(metaclass=ABCMeta):
@abstractmethod
def get_map(self) -> Mapping[FileDescriptorLike, SelectorKey]: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...

class SelectSelector(BaseSelector):
def register(self, fileobj: FileDescriptorLike, events: _EventMask, data: Any = ...) -> SelectorKey: ...
Expand Down
5 changes: 4 additions & 1 deletion stdlib/sqlite3/dbapi2.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from _typeshed import Self, StrOrBytesPath
from datetime import date, datetime, time
from types import TracebackType
from typing import Any, Callable, Generator, Iterable, Iterator, Protocol, TypeVar
from typing_extensions import Literal, final

Expand Down Expand Up @@ -183,7 +184,9 @@ class Connection:

def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, __type: type | None, __value: BaseException | None, __traceback: Any | None) -> Literal[False]: ...
def __exit__(
self, __type: type[BaseException] | None, __value: BaseException | None, __traceback: TracebackType | None
) -> Literal[False]: ...

class Cursor(Iterator[Any]):
arraysize: Any
Expand Down
4 changes: 2 additions & 2 deletions stdlib/sunau.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class _sunau_params(NamedTuple):
class Au_read:
def __init__(self, f: _File) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
def getfp(self) -> IO[bytes] | None: ...
def rewind(self) -> None: ...
def close(self) -> None: ...
Expand All @@ -53,7 +53,7 @@ class Au_read:
class Au_write:
def __init__(self, f: _File) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
def setnchannels(self, nchannels: int) -> None: ...
def getnchannels(self) -> int: ...
def setsampwidth(self, sampwidth: int) -> None: ...
Expand Down
5 changes: 4 additions & 1 deletion stdlib/telnetlib.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket
from _typeshed import Self
from types import TracebackType
from typing import Any, Callable, Match, Pattern, Sequence

__all__ = ["Telnet"]
Expand Down Expand Up @@ -113,4 +114,6 @@ class Telnet:
self, list: Sequence[Pattern[bytes] | bytes], timeout: float | None = ...
) -> tuple[int, Match[bytes] | None, bytes]: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ...
def __exit__(
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> None: ...
7 changes: 5 additions & 2 deletions stdlib/unittest/mock.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from _typeshed import Self
from contextlib import _GeneratorContextManager
from types import TracebackType
from typing import Any, Awaitable, Callable, Generic, Iterable, Mapping, Sequence, TypeVar, overload
from typing_extensions import Literal

Expand Down Expand Up @@ -263,7 +264,9 @@ class _patch(Generic[_T]):
temp_original: Any
is_local: bool
def __enter__(self) -> _T: ...
def __exit__(self, *exc_info: Any) -> None: ...
def __exit__(
self, __exc_type: type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None
) -> None: ...
def start(self) -> _T: ...
def stop(self) -> None: ...

Expand All @@ -275,7 +278,7 @@ class _patch_dict:
def __call__(self, f: Any) -> Any: ...
def decorate_class(self, klass: Any) -> Any: ...
def __enter__(self) -> Any: ...
def __exit__(self, *args: Any) -> Any: ...
def __exit__(self, *args: object) -> Any: ...
start: Any
stop: Any

Expand Down
4 changes: 2 additions & 2 deletions stdlib/wave.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class _wave_params(NamedTuple):
class Wave_read:
def __init__(self, f: _File) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
def getfp(self) -> BinaryIO | None: ...
def rewind(self) -> None: ...
def close(self) -> None: ...
Expand All @@ -45,7 +45,7 @@ class Wave_read:
class Wave_write:
def __init__(self, f: _File) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: Any) -> None: ...
def __exit__(self, *args: object) -> None: ...
def setnchannels(self, nchannels: int) -> None: ...
def getnchannels(self) -> int: ...
def setsampwidth(self, sampwidth: int) -> None: ...
Expand Down