diff --git a/stdlib/http/client.pyi b/stdlib/http/client.pyi index c35228928d77..1914daf1d93c 100644 --- a/stdlib/http/client.pyi +++ b/stdlib/http/client.pyi @@ -3,7 +3,7 @@ import io import ssl import sys import types -from _typeshed import WriteableBuffer +from _typeshed import Self, WriteableBuffer from socket import socket from typing import ( IO, @@ -115,7 +115,7 @@ class HTTPResponse(io.BufferedIOBase, BinaryIO): def fileno(self) -> int: ... def isclosed(self) -> bool: ... def __iter__(self) -> Iterator[bytes]: ... - def __enter__(self) -> HTTPResponse: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[types.TracebackType] ) -> Optional[bool]: ... diff --git a/stdlib/multiprocessing/connection.pyi b/stdlib/multiprocessing/connection.pyi index c87dd310e37f..ce56bea53bd7 100644 --- a/stdlib/multiprocessing/connection.pyi +++ b/stdlib/multiprocessing/connection.pyi @@ -29,7 +29,7 @@ class _ConnectionBase: def recv_bytes_into(self, buf: Any, offset: int = ...) -> int: ... def recv(self) -> Any: ... def poll(self, timeout: Optional[float] = ...) -> bool: ... - def __enter__(self) -> _ConnectionBase: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], exc_tb: Optional[types.TracebackType] ) -> None: ... diff --git a/stdlib/multiprocessing/dummy/connection.pyi b/stdlib/multiprocessing/dummy/connection.pyi index 01733e59c763..d3abec749cde 100644 --- a/stdlib/multiprocessing/dummy/connection.pyi +++ b/stdlib/multiprocessing/dummy/connection.pyi @@ -1,11 +1,10 @@ +from _typeshed import Self from queue import Queue from types import TracebackType -from typing import Any, List, Optional, Tuple, Type, TypeVar, Union +from typing import Any, List, Optional, Tuple, Type, Union families: List[None] -_ConnectionT = TypeVar("_ConnectionT", bound=Connection) -_ListenerT = TypeVar("_ListenerT", bound=Listener) _Address = Union[str, Tuple[str, int]] class Connection(object): @@ -15,7 +14,7 @@ class Connection(object): recv_bytes: Any send: Any send_bytes: Any - def __enter__(self: _ConnectionT) -> _ConnectionT: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> None: ... @@ -27,7 +26,7 @@ class Listener(object): _backlog_queue: Optional[Queue[Any]] @property def address(self) -> Optional[Queue[Any]]: ... - def __enter__(self: _ListenerT) -> _ListenerT: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> None: ... diff --git a/stdlib/multiprocessing/pool.pyi b/stdlib/multiprocessing/pool.pyi index d8004655cacf..0f4d33dc376f 100644 --- a/stdlib/multiprocessing/pool.pyi +++ b/stdlib/multiprocessing/pool.pyi @@ -1,4 +1,5 @@ import sys +from _typeshed import Self from typing import Any, Callable, ContextManager, Generic, Iterable, Iterator, List, Mapping, Optional, TypeVar if sys.version_info >= (3, 9): @@ -77,7 +78,7 @@ class Pool(ContextManager[Pool]): def close(self) -> None: ... def terminate(self) -> None: ... def join(self) -> None: ... - def __enter__(self: _PT) -> _PT: ... + def __enter__(self: Self) -> Self: ... class ThreadPool(Pool, ContextManager[ThreadPool]): def __init__( diff --git a/stdlib/os/__init__.pyi b/stdlib/os/__init__.pyi index 5de46ca031d7..24c6d7000e58 100644 --- a/stdlib/os/__init__.pyi +++ b/stdlib/os/__init__.pyi @@ -6,6 +6,7 @@ from _typeshed import ( OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode, + Self, StrOrBytesPath, StrPath, ) @@ -860,7 +861,7 @@ if sys.version_info >= (3, 8): path: Optional[str] def __init__(self, path: Optional[str], cookie: _T, remove_dll_directory: Callable[[_T], Any]) -> None: ... def close(self) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, *args: Any) -> None: ... def add_dll_directory(path: str) -> _AddedDllDirectory: ... if sys.platform == "linux": diff --git a/stdlib/sqlite3/dbapi2.pyi b/stdlib/sqlite3/dbapi2.pyi index c53141856ca8..3fb72de659d2 100644 --- a/stdlib/sqlite3/dbapi2.pyi +++ b/stdlib/sqlite3/dbapi2.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import StrOrBytesPath +from _typeshed import Self, StrOrBytesPath from datetime import date, datetime, time from typing import Any, Callable, Generator, Iterable, Iterator, List, Optional, Protocol, Tuple, Type, TypeVar, Union @@ -158,7 +158,7 @@ class Connection(object): sleep: float = ..., ) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... - def __enter__(self) -> Connection: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, t: Optional[type], exc: Optional[BaseException], tb: Optional[Any]) -> None: ... class Cursor(Iterator[Any]): diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 16036264af11..79a7e79069af 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -2,6 +2,7 @@ import datetime import logging import sys import unittest.result +from _typeshed import Self from collections.abc import Set from types import TracebackType from typing import ( @@ -14,6 +15,7 @@ from typing import ( Iterable, List, Mapping, + NamedTuple, NoReturn, Optional, Pattern, @@ -251,9 +253,13 @@ class FunctionTestCase(TestCase): ) -> None: ... def runTest(self) -> None: ... +class _LoggingWatcher(NamedTuple): + records: List[logging.LogRecord] + output: List[str] + class _AssertRaisesContext(Generic[_E]): exception: _E - def __enter__(self) -> _AssertRaisesContext[_E]: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> bool: ... @@ -265,7 +271,7 @@ class _AssertWarnsContext: filename: str lineno: int warnings: List[WarningMessage] - def __enter__(self) -> _AssertWarnsContext: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> None: ... @@ -275,7 +281,10 @@ class _AssertLogsContext: records: List[logging.LogRecord] output: List[str] def __init__(self, test_case: TestCase, logger_name: str, level: int) -> None: ... - def __enter__(self) -> _AssertLogsContext: ... + if sys.version_info >= (3, 10): + def __enter__(self) -> _LoggingWatcher | None: ... + else: + def __enter__(self) -> _LoggingWatcher: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> Optional[bool]: ... diff --git a/stdlib/urllib/response.pyi b/stdlib/urllib/response.pyi index c8f9d75d2f14..300105076048 100644 --- a/stdlib/urllib/response.pyi +++ b/stdlib/urllib/response.pyi @@ -1,3 +1,4 @@ +from _typeshed import Self from email.message import Message from types import TracebackType from typing import IO, Any, BinaryIO, Callable, Iterable, List, Optional, Tuple, Type, TypeVar @@ -7,7 +8,7 @@ _AIUT = TypeVar("_AIUT", bound=addbase) class addbase(BinaryIO): fp: IO[bytes] def __init__(self, fp: IO[bytes]) -> None: ... - def __enter__(self: _AIUT) -> _AIUT: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType] ) -> None: ... diff --git a/stdlib/xml/dom/minidom.pyi b/stdlib/xml/dom/minidom.pyi index 90396299effc..a9ae79c01902 100644 --- a/stdlib/xml/dom/minidom.pyi +++ b/stdlib/xml/dom/minidom.pyi @@ -1,11 +1,10 @@ import sys import xml.dom -from typing import IO, Any, Optional, TypeVar, Union +from _typeshed import Self +from typing import IO, Any, Optional, Union from xml.dom.xmlbuilder import DocumentLS, DOMImplementationLS from xml.sax.xmlreader import XMLReader -_T = TypeVar("_T") - def parse(file: Union[str, IO[Any]], parser: Optional[XMLReader] = ..., bufsize: Optional[int] = ...): ... def parseString(string: Union[str, bytes], parser: Optional[XMLReader] = ...): ... def getDOMImplementation(features=...): ... @@ -39,7 +38,7 @@ class Node(xml.dom.Node): def setUserData(self, key, data, handler): ... childNodes: Any def unlink(self) -> None: ... - def __enter__(self: _T) -> _T: ... + def __enter__(self: Self) -> Self: ... def __exit__(self, et, ev, tb) -> None: ... class DocumentFragment(Node): diff --git a/stdlib/xmlrpc/client.pyi b/stdlib/xmlrpc/client.pyi index b0c615063729..8844b875950e 100644 --- a/stdlib/xmlrpc/client.pyi +++ b/stdlib/xmlrpc/client.pyi @@ -2,7 +2,7 @@ import gzip import http.client import sys import time -from _typeshed import SupportsRead, SupportsWrite +from _typeshed import Self, SupportsRead, SupportsWrite from datetime import datetime from io import BytesIO from types import TracebackType @@ -301,7 +301,7 @@ class ServerProxy: def __call__(self, attr: Literal["transport"]) -> Transport: ... @overload def __call__(self, attr: str) -> Union[Callable[[], None], Transport]: ... - def __enter__(self) -> ServerProxy: ... + def __enter__(self: Self) -> Self: ... def __exit__( self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] ) -> None: ...