Skip to content

Commit 8a10746

Browse files
authored
Use _typeshed.Self with __enter__ (#5719)
1 parent 96e0660 commit 8a10746

File tree

11 files changed

+30
-45
lines changed

11 files changed

+30
-45
lines changed

stdlib/asyncio/unix_events.pyi

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import sys
22
import types
3+
from _typeshed import Self
34
from socket import socket
4-
from typing import Any, Callable, Optional, Type, TypeVar
5+
from typing import Any, Callable, Optional, Type
56

67
from .base_events import Server
78
from .events import AbstractEventLoop, BaseDefaultEventLoopPolicy, _ProtocolFactory, _SSLContext
89
from .selector_events import BaseSelectorEventLoop
910

10-
_T1 = TypeVar("_T1", bound=AbstractChildWatcher)
11-
_T2 = TypeVar("_T2", bound=SafeChildWatcher)
12-
_T3 = TypeVar("_T3", bound=FastChildWatcher)
13-
1411
class AbstractChildWatcher:
1512
def add_child_handler(self, pid: int, callback: Callable[..., Any], *args: Any) -> None: ...
1613
def remove_child_handler(self, pid: int) -> bool: ...
1714
def attach_loop(self, loop: Optional[AbstractEventLoop]) -> None: ...
1815
def close(self) -> None: ...
19-
def __enter__(self: _T1) -> _T1: ...
16+
def __enter__(self: Self) -> Self: ...
2017
def __exit__(
2118
self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType]
2219
) -> None: ...
@@ -27,10 +24,10 @@ class BaseChildWatcher(AbstractChildWatcher):
2724
def __init__(self) -> None: ...
2825

2926
class SafeChildWatcher(BaseChildWatcher):
30-
def __enter__(self: _T2) -> _T2: ...
27+
def __enter__(self: Self) -> Self: ...
3128

3229
class FastChildWatcher(BaseChildWatcher):
33-
def __enter__(self: _T3) -> _T3: ...
30+
def __enter__(self: Self) -> Self: ...
3431

3532
class _UnixSelectorEventLoop(BaseSelectorEventLoop):
3633
if sys.version_info < (3, 7):
@@ -55,15 +52,12 @@ DefaultEventLoopPolicy = _UnixDefaultEventLoopPolicy
5552
if sys.version_info >= (3, 8):
5653

5754
from typing import Protocol
58-
59-
_T4 = TypeVar("_T4", bound=MultiLoopChildWatcher)
60-
_T5 = TypeVar("_T5", bound=ThreadedChildWatcher)
6155
class _Warn(Protocol):
6256
def __call__(
6357
self, message: str, category: Optional[Type[Warning]] = ..., stacklevel: int = ..., source: Optional[Any] = ...
6458
) -> None: ...
6559
class MultiLoopChildWatcher(AbstractChildWatcher):
66-
def __enter__(self: _T4) -> _T4: ...
60+
def __enter__(self: Self) -> Self: ...
6761
class ThreadedChildWatcher(AbstractChildWatcher):
68-
def __enter__(self: _T5) -> _T5: ...
62+
def __enter__(self: Self) -> Self: ...
6963
def __del__(self, _warn: _Warn = ...) -> None: ...

stdlib/concurrent/futures/_base.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import threading
3+
from _typeshed import Self
34
from abc import abstractmethod
45
from logging import Logger
56
from typing import (
@@ -79,7 +80,7 @@ class Executor:
7980
def shutdown(self, wait: bool = ..., *, cancel_futures: bool = ...) -> None: ...
8081
else:
8182
def shutdown(self, wait: bool = ...) -> None: ...
82-
def __enter__(self: _T) -> _T: ...
83+
def __enter__(self: Self) -> Self: ...
8384
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Optional[bool]: ...
8485

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

stdlib/dbm/__init__.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 types import TracebackType
23
from typing import Iterator, MutableMapping, Optional, Tuple, Type, Union
34
from typing_extensions import Literal
@@ -79,7 +80,7 @@ class _Database(MutableMapping[_KeyType, bytes]):
7980
def __iter__(self) -> Iterator[bytes]: ...
8081
def __len__(self) -> int: ...
8182
def __del__(self) -> None: ...
82-
def __enter__(self) -> _Database: ...
83+
def __enter__(self: Self) -> Self: ...
8384
def __exit__(
8485
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
8586
) -> None: ...

stdlib/dbm/dumb.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 types import TracebackType
23
from typing import Iterator, MutableMapping, Optional, Type, Union
34

@@ -17,7 +18,7 @@ class _Database(MutableMapping[_KeyType, bytes]):
1718
def __iter__(self) -> Iterator[bytes]: ...
1819
def __len__(self) -> int: ...
1920
def __del__(self) -> None: ...
20-
def __enter__(self) -> _Database: ...
21+
def __enter__(self: Self) -> Self: ...
2122
def __exit__(
2223
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
2324
) -> None: ...

stdlib/dbm/gnu.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 types import TracebackType
23
from typing import List, Optional, Type, TypeVar, Union, overload
34

@@ -19,7 +20,7 @@ class _gdbm:
1920
def __delitem__(self, key: _KeyType) -> None: ...
2021
def __contains__(self, key: _KeyType) -> bool: ...
2122
def __len__(self) -> int: ...
22-
def __enter__(self) -> _gdbm: ...
23+
def __enter__(self: Self) -> Self: ...
2324
def __exit__(
2425
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
2526
) -> None: ...

stdlib/dbm/ndbm.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 types import TracebackType
23
from typing import List, Optional, Type, TypeVar, Union, overload
34

@@ -17,7 +18,7 @@ class _dbm:
1718
def __delitem__(self, key: _KeyType) -> None: ...
1819
def __len__(self) -> int: ...
1920
def __del__(self) -> None: ...
20-
def __enter__(self) -> _dbm: ...
21+
def __enter__(self: Self) -> Self: ...
2122
def __exit__(
2223
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
2324
) -> None: ...

stdlib/tarfile.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bz2
22
import io
33
import sys
4-
from _typeshed import StrOrBytesPath, StrPath
4+
from _typeshed import Self, StrOrBytesPath, StrPath
55
from collections.abc import Callable, Iterable, Iterator, Mapping
66
from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
77
from types import TracebackType
@@ -124,7 +124,7 @@ class TarFile:
124124
errorlevel: Optional[int] = ...,
125125
copybufsize: Optional[int] = ..., # undocumented
126126
) -> None: ...
127-
def __enter__(self: _TF) -> _TF: ...
127+
def __enter__(self: Self) -> Self: ...
128128
def __exit__(
129129
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
130130
) -> None: ...

stdlib/telnetlib.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import socket
2+
from _typeshed import Self
23
from typing import Any, Callable, Match, Optional, Pattern, Sequence, Tuple, Union
34

45
DEBUGLEVEL: int
@@ -109,5 +110,5 @@ class Telnet:
109110
def expect(
110111
self, list: Sequence[Union[Pattern[bytes], bytes]], timeout: Optional[float] = ...
111112
) -> Tuple[int, Optional[Match[bytes]], bytes]: ...
112-
def __enter__(self) -> Telnet: ...
113+
def __enter__(self: Self) -> Self: ...
113114
def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ...

stdlib/tempfile.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
import sys
3+
from _typeshed import Self
34
from types import TracebackType
4-
from typing import IO, Any, AnyStr, Generic, Iterable, Iterator, List, Optional, Tuple, Type, TypeVar, Union, overload
5+
from typing import IO, Any, AnyStr, Generic, Iterable, Iterator, List, Optional, Tuple, Type, Union, overload
56
from typing_extensions import Literal
67

78
if sys.version_info >= (3, 9):
@@ -12,7 +13,6 @@ TMP_MAX: int
1213
tempdir: Optional[str]
1314
template: str
1415

15-
_S = TypeVar("_S")
1616
_DirT = Union[AnyStr, os.PathLike[AnyStr]]
1717

1818
if sys.version_info >= (3, 8):
@@ -168,7 +168,7 @@ class _TemporaryFileWrapper(Generic[AnyStr], IO[AnyStr]):
168168
name: str
169169
delete: bool
170170
def __init__(self, file: IO[AnyStr], name: str, delete: bool = ...) -> None: ...
171-
def __enter__(self) -> _TemporaryFileWrapper[AnyStr]: ...
171+
def __enter__(self: Self) -> Self: ...
172172
def __exit__(
173173
self, exc: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[TracebackType]
174174
) -> Optional[bool]: ...
@@ -289,7 +289,7 @@ class SpooledTemporaryFile(IO[AnyStr]):
289289
dir: Optional[str] = ...,
290290
) -> None: ...
291291
def rollover(self) -> None: ...
292-
def __enter__(self: _S) -> _S: ...
292+
def __enter__(self: Self) -> Self: ...
293293
def __exit__(
294294
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
295295
) -> Optional[bool]: ...

stdlib/winreg.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 types import TracebackType
23
from typing import Any, Optional, Tuple, Type, Union
34

@@ -90,7 +91,7 @@ error = OSError
9091
class HKEYType:
9192
def __bool__(self) -> bool: ...
9293
def __int__(self) -> int: ...
93-
def __enter__(self) -> HKEYType: ...
94+
def __enter__(self: Self) -> Self: ...
9495
def __exit__(
9596
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
9697
) -> Optional[bool]: ...

stdlib/zipfile.pyi

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
import io
22
import sys
3-
from _typeshed import StrPath
3+
from _typeshed import Self, StrPath
44
from types import TracebackType
5-
from typing import (
6-
IO,
7-
Callable,
8-
Dict,
9-
Iterable,
10-
Iterator,
11-
List,
12-
Optional,
13-
Protocol,
14-
Sequence,
15-
Tuple,
16-
Type,
17-
TypeVar,
18-
Union,
19-
overload,
20-
)
5+
from typing import IO, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload
216
from typing_extensions import Literal
227

23-
_T = TypeVar("_T")
248
_DateTuple = Tuple[int, int, int, int, int, int]
259

2610
class BadZipFile(Exception): ...
@@ -144,7 +128,7 @@ class ZipFile:
144128
def __init__(
145129
self, file: Union[StrPath, IO[bytes]], mode: str = ..., compression: int = ..., allowZip64: bool = ...
146130
) -> None: ...
147-
def __enter__(self: _T) -> _T: ...
131+
def __enter__(self: Self) -> Self: ...
148132
def __exit__(
149133
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
150134
) -> None: ...

0 commit comments

Comments
 (0)