Skip to content

Commit d68701c

Browse files
gruebelsrittauAkuli
authored
Use _typeshed.Self with __enter__ (#5723)
Co-authored-by: Sebastian Rittau <[email protected]> Co-authored-by: Akuli <[email protected]>
1 parent 1a131a4 commit d68701c

File tree

10 files changed

+32
-22
lines changed

10 files changed

+32
-22
lines changed

stdlib/http/client.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import io
33
import ssl
44
import sys
55
import types
6-
from _typeshed import WriteableBuffer
6+
from _typeshed import Self, WriteableBuffer
77
from socket import socket
88
from typing import (
99
IO,
@@ -115,7 +115,7 @@ class HTTPResponse(io.BufferedIOBase, BinaryIO):
115115
def fileno(self) -> int: ...
116116
def isclosed(self) -> bool: ...
117117
def __iter__(self) -> Iterator[bytes]: ...
118-
def __enter__(self) -> HTTPResponse: ...
118+
def __enter__(self: Self) -> Self: ...
119119
def __exit__(
120120
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[types.TracebackType]
121121
) -> Optional[bool]: ...

stdlib/multiprocessing/connection.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class _ConnectionBase:
2929
def recv_bytes_into(self, buf: Any, offset: int = ...) -> int: ...
3030
def recv(self) -> Any: ...
3131
def poll(self, timeout: Optional[float] = ...) -> bool: ...
32-
def __enter__(self) -> _ConnectionBase: ...
32+
def __enter__(self: Self) -> Self: ...
3333
def __exit__(
3434
self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], exc_tb: Optional[types.TracebackType]
3535
) -> None: ...

stdlib/multiprocessing/dummy/connection.pyi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
from _typeshed import Self
12
from queue import Queue
23
from types import TracebackType
3-
from typing import Any, List, Optional, Tuple, Type, TypeVar, Union
4+
from typing import Any, List, Optional, Tuple, Type, Union
45

56
families: List[None]
67

7-
_ConnectionT = TypeVar("_ConnectionT", bound=Connection)
8-
_ListenerT = TypeVar("_ListenerT", bound=Listener)
98
_Address = Union[str, Tuple[str, int]]
109

1110
class Connection(object):
@@ -15,7 +14,7 @@ class Connection(object):
1514
recv_bytes: Any
1615
send: Any
1716
send_bytes: Any
18-
def __enter__(self: _ConnectionT) -> _ConnectionT: ...
17+
def __enter__(self: Self) -> Self: ...
1918
def __exit__(
2019
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
2120
) -> None: ...
@@ -27,7 +26,7 @@ class Listener(object):
2726
_backlog_queue: Optional[Queue[Any]]
2827
@property
2928
def address(self) -> Optional[Queue[Any]]: ...
30-
def __enter__(self: _ListenerT) -> _ListenerT: ...
29+
def __enter__(self: Self) -> Self: ...
3130
def __exit__(
3231
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
3332
) -> None: ...

stdlib/multiprocessing/pool.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from _typeshed import Self
23
from typing import Any, Callable, ContextManager, Generic, Iterable, Iterator, List, Mapping, Optional, TypeVar
34

45
if sys.version_info >= (3, 9):
@@ -77,7 +78,7 @@ class Pool(ContextManager[Pool]):
7778
def close(self) -> None: ...
7879
def terminate(self) -> None: ...
7980
def join(self) -> None: ...
80-
def __enter__(self: _PT) -> _PT: ...
81+
def __enter__(self: Self) -> Self: ...
8182

8283
class ThreadPool(Pool, ContextManager[ThreadPool]):
8384
def __init__(

stdlib/os/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from _typeshed import (
66
OpenBinaryModeUpdating,
77
OpenBinaryModeWriting,
88
OpenTextMode,
9+
Self,
910
StrOrBytesPath,
1011
StrPath,
1112
)
@@ -860,7 +861,7 @@ if sys.version_info >= (3, 8):
860861
path: Optional[str]
861862
def __init__(self, path: Optional[str], cookie: _T, remove_dll_directory: Callable[[_T], Any]) -> None: ...
862863
def close(self) -> None: ...
863-
def __enter__(self: _T) -> _T: ...
864+
def __enter__(self: Self) -> Self: ...
864865
def __exit__(self, *args: Any) -> None: ...
865866
def add_dll_directory(path: str) -> _AddedDllDirectory: ...
866867
if sys.platform == "linux":

stdlib/sqlite3/dbapi2.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from _typeshed import StrOrBytesPath
2+
from _typeshed import Self, StrOrBytesPath
33
from datetime import date, datetime, time
44
from typing import Any, Callable, Generator, Iterable, Iterator, List, Optional, Protocol, Tuple, Type, TypeVar, Union
55

@@ -158,7 +158,7 @@ class Connection(object):
158158
sleep: float = ...,
159159
) -> None: ...
160160
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
161-
def __enter__(self) -> Connection: ...
161+
def __enter__(self: Self) -> Self: ...
162162
def __exit__(self, t: Optional[type], exc: Optional[BaseException], tb: Optional[Any]) -> None: ...
163163

164164
class Cursor(Iterator[Any]):

stdlib/unittest/case.pyi

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import datetime
22
import logging
33
import sys
44
import unittest.result
5+
from _typeshed import Self
56
from collections.abc import Set
67
from types import TracebackType
78
from typing import (
@@ -14,6 +15,7 @@ from typing import (
1415
Iterable,
1516
List,
1617
Mapping,
18+
NamedTuple,
1719
NoReturn,
1820
Optional,
1921
Pattern,
@@ -251,9 +253,13 @@ class FunctionTestCase(TestCase):
251253
) -> None: ...
252254
def runTest(self) -> None: ...
253255

256+
class _LoggingWatcher(NamedTuple):
257+
records: List[logging.LogRecord]
258+
output: List[str]
259+
254260
class _AssertRaisesContext(Generic[_E]):
255261
exception: _E
256-
def __enter__(self) -> _AssertRaisesContext[_E]: ...
262+
def __enter__(self: Self) -> Self: ...
257263
def __exit__(
258264
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
259265
) -> bool: ...
@@ -265,7 +271,7 @@ class _AssertWarnsContext:
265271
filename: str
266272
lineno: int
267273
warnings: List[WarningMessage]
268-
def __enter__(self) -> _AssertWarnsContext: ...
274+
def __enter__(self: Self) -> Self: ...
269275
def __exit__(
270276
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
271277
) -> None: ...
@@ -275,7 +281,10 @@ class _AssertLogsContext:
275281
records: List[logging.LogRecord]
276282
output: List[str]
277283
def __init__(self, test_case: TestCase, logger_name: str, level: int) -> None: ...
278-
def __enter__(self) -> _AssertLogsContext: ...
284+
if sys.version_info >= (3, 10):
285+
def __enter__(self) -> _LoggingWatcher | None: ...
286+
else:
287+
def __enter__(self) -> _LoggingWatcher: ...
279288
def __exit__(
280289
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
281290
) -> Optional[bool]: ...

stdlib/urllib/response.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import Self
12
from email.message import Message
23
from types import TracebackType
34
from typing import IO, Any, BinaryIO, Callable, Iterable, List, Optional, Tuple, Type, TypeVar
@@ -7,7 +8,7 @@ _AIUT = TypeVar("_AIUT", bound=addbase)
78
class addbase(BinaryIO):
89
fp: IO[bytes]
910
def __init__(self, fp: IO[bytes]) -> None: ...
10-
def __enter__(self: _AIUT) -> _AIUT: ...
11+
def __enter__(self: Self) -> Self: ...
1112
def __exit__(
1213
self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]
1314
) -> None: ...

stdlib/xml/dom/minidom.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import sys
22
import xml.dom
3-
from typing import IO, Any, Optional, TypeVar, Union
3+
from _typeshed import Self
4+
from typing import IO, Any, Optional, Union
45
from xml.dom.xmlbuilder import DocumentLS, DOMImplementationLS
56
from xml.sax.xmlreader import XMLReader
67

7-
_T = TypeVar("_T")
8-
98
def parse(file: Union[str, IO[Any]], parser: Optional[XMLReader] = ..., bufsize: Optional[int] = ...): ...
109
def parseString(string: Union[str, bytes], parser: Optional[XMLReader] = ...): ...
1110
def getDOMImplementation(features=...): ...
@@ -39,7 +38,7 @@ class Node(xml.dom.Node):
3938
def setUserData(self, key, data, handler): ...
4039
childNodes: Any
4140
def unlink(self) -> None: ...
42-
def __enter__(self: _T) -> _T: ...
41+
def __enter__(self: Self) -> Self: ...
4342
def __exit__(self, et, ev, tb) -> None: ...
4443

4544
class DocumentFragment(Node):

stdlib/xmlrpc/client.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import gzip
22
import http.client
33
import sys
44
import time
5-
from _typeshed import SupportsRead, SupportsWrite
5+
from _typeshed import Self, SupportsRead, SupportsWrite
66
from datetime import datetime
77
from io import BytesIO
88
from types import TracebackType
@@ -301,7 +301,7 @@ class ServerProxy:
301301
def __call__(self, attr: Literal["transport"]) -> Transport: ...
302302
@overload
303303
def __call__(self, attr: str) -> Union[Callable[[], None], Transport]: ...
304-
def __enter__(self) -> ServerProxy: ...
304+
def __enter__(self: Self) -> Self: ...
305305
def __exit__(
306306
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
307307
) -> None: ...

0 commit comments

Comments
 (0)