Skip to content

Commit

Permalink
Use _typeshed.Self with __enter__ (#5719)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruebel authored Jul 1, 2021
1 parent 96e0660 commit 8a10746
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 45 deletions.
20 changes: 7 additions & 13 deletions stdlib/asyncio/unix_events.pyi
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import sys
import types
from _typeshed import Self
from socket import socket
from typing import Any, Callable, Optional, Type, TypeVar
from typing import Any, Callable, Optional, Type

from .base_events import Server
from .events import AbstractEventLoop, BaseDefaultEventLoopPolicy, _ProtocolFactory, _SSLContext
from .selector_events import BaseSelectorEventLoop

_T1 = TypeVar("_T1", bound=AbstractChildWatcher)
_T2 = TypeVar("_T2", bound=SafeChildWatcher)
_T3 = TypeVar("_T3", bound=FastChildWatcher)

class AbstractChildWatcher:
def add_child_handler(self, pid: int, callback: Callable[..., Any], *args: Any) -> None: ...
def remove_child_handler(self, pid: int) -> bool: ...
def attach_loop(self, loop: Optional[AbstractEventLoop]) -> None: ...
def close(self) -> None: ...
def __enter__(self: _T1) -> _T1: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, typ: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[types.TracebackType]
) -> None: ...
Expand All @@ -27,10 +24,10 @@ class BaseChildWatcher(AbstractChildWatcher):
def __init__(self) -> None: ...

class SafeChildWatcher(BaseChildWatcher):
def __enter__(self: _T2) -> _T2: ...
def __enter__(self: Self) -> Self: ...

class FastChildWatcher(BaseChildWatcher):
def __enter__(self: _T3) -> _T3: ...
def __enter__(self: Self) -> Self: ...

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

from typing import Protocol

_T4 = TypeVar("_T4", bound=MultiLoopChildWatcher)
_T5 = TypeVar("_T5", bound=ThreadedChildWatcher)
class _Warn(Protocol):
def __call__(
self, message: str, category: Optional[Type[Warning]] = ..., stacklevel: int = ..., source: Optional[Any] = ...
) -> None: ...
class MultiLoopChildWatcher(AbstractChildWatcher):
def __enter__(self: _T4) -> _T4: ...
def __enter__(self: Self) -> Self: ...
class ThreadedChildWatcher(AbstractChildWatcher):
def __enter__(self: _T5) -> _T5: ...
def __enter__(self: Self) -> Self: ...
def __del__(self, _warn: _Warn = ...) -> None: ...
3 changes: 2 additions & 1 deletion stdlib/concurrent/futures/_base.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import threading
from _typeshed import Self
from abc import abstractmethod
from logging import Logger
from typing import (
Expand Down Expand Up @@ -79,7 +80,7 @@ class Executor:
def shutdown(self, wait: bool = ..., *, cancel_futures: bool = ...) -> None: ...
else:
def shutdown(self, wait: bool = ...) -> None: ...
def __enter__(self: _T) -> _T: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Optional[bool]: ...

def as_completed(fs: Iterable[Future[_T]], timeout: Optional[float] = ...) -> Iterator[Future[_T]]: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/dbm/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from types import TracebackType
from typing import Iterator, MutableMapping, Optional, Tuple, Type, Union
from typing_extensions import Literal
Expand Down Expand Up @@ -79,7 +80,7 @@ class _Database(MutableMapping[_KeyType, bytes]):
def __iter__(self) -> Iterator[bytes]: ...
def __len__(self) -> int: ...
def __del__(self) -> None: ...
def __enter__(self) -> _Database: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/dbm/dumb.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from types import TracebackType
from typing import Iterator, MutableMapping, Optional, Type, Union

Expand All @@ -17,7 +18,7 @@ class _Database(MutableMapping[_KeyType, bytes]):
def __iter__(self) -> Iterator[bytes]: ...
def __len__(self) -> int: ...
def __del__(self) -> None: ...
def __enter__(self) -> _Database: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/dbm/gnu.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from types import TracebackType
from typing import List, Optional, Type, TypeVar, Union, overload

Expand All @@ -19,7 +20,7 @@ class _gdbm:
def __delitem__(self, key: _KeyType) -> None: ...
def __contains__(self, key: _KeyType) -> bool: ...
def __len__(self) -> int: ...
def __enter__(self) -> _gdbm: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/dbm/ndbm.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from types import TracebackType
from typing import List, Optional, Type, TypeVar, Union, overload

Expand All @@ -17,7 +18,7 @@ class _dbm:
def __delitem__(self, key: _KeyType) -> None: ...
def __len__(self) -> int: ...
def __del__(self) -> None: ...
def __enter__(self) -> _dbm: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bz2
import io
import sys
from _typeshed import StrOrBytesPath, StrPath
from _typeshed import Self, StrOrBytesPath, StrPath
from collections.abc import Callable, Iterable, Iterator, Mapping
from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
from types import TracebackType
Expand Down Expand Up @@ -124,7 +124,7 @@ class TarFile:
errorlevel: Optional[int] = ...,
copybufsize: Optional[int] = ..., # undocumented
) -> None: ...
def __enter__(self: _TF) -> _TF: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/telnetlib.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import socket
from _typeshed import Self
from typing import Any, Callable, Match, Optional, Pattern, Sequence, Tuple, Union

DEBUGLEVEL: int
Expand Down Expand Up @@ -109,5 +110,5 @@ class Telnet:
def expect(
self, list: Sequence[Union[Pattern[bytes], bytes]], timeout: Optional[float] = ...
) -> Tuple[int, Optional[Match[bytes]], bytes]: ...
def __enter__(self) -> Telnet: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ...
8 changes: 4 additions & 4 deletions stdlib/tempfile.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import sys
from _typeshed import Self
from types import TracebackType
from typing import IO, Any, AnyStr, Generic, Iterable, Iterator, List, Optional, Tuple, Type, TypeVar, Union, overload
from typing import IO, Any, AnyStr, Generic, Iterable, Iterator, List, Optional, Tuple, Type, Union, overload
from typing_extensions import Literal

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

_S = TypeVar("_S")
_DirT = Union[AnyStr, os.PathLike[AnyStr]]

if sys.version_info >= (3, 8):
Expand Down Expand Up @@ -168,7 +168,7 @@ class _TemporaryFileWrapper(Generic[AnyStr], IO[AnyStr]):
name: str
delete: bool
def __init__(self, file: IO[AnyStr], name: str, delete: bool = ...) -> None: ...
def __enter__(self) -> _TemporaryFileWrapper[AnyStr]: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[TracebackType]
) -> Optional[bool]: ...
Expand Down Expand Up @@ -289,7 +289,7 @@ class SpooledTemporaryFile(IO[AnyStr]):
dir: Optional[str] = ...,
) -> None: ...
def rollover(self) -> None: ...
def __enter__(self: _S) -> _S: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> Optional[bool]: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/winreg.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import Self
from types import TracebackType
from typing import Any, Optional, Tuple, Type, Union

Expand Down Expand Up @@ -90,7 +91,7 @@ error = OSError
class HKEYType:
def __bool__(self) -> bool: ...
def __int__(self) -> int: ...
def __enter__(self) -> HKEYType: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> Optional[bool]: ...
Expand Down
22 changes: 3 additions & 19 deletions stdlib/zipfile.pyi
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
import io
import sys
from _typeshed import StrPath
from _typeshed import Self, StrPath
from types import TracebackType
from typing import (
IO,
Callable,
Dict,
Iterable,
Iterator,
List,
Optional,
Protocol,
Sequence,
Tuple,
Type,
TypeVar,
Union,
overload,
)
from typing import IO, Callable, Dict, Iterable, Iterator, List, Optional, Protocol, Sequence, Tuple, Type, Union, overload
from typing_extensions import Literal

_T = TypeVar("_T")
_DateTuple = Tuple[int, int, int, int, int, int]

class BadZipFile(Exception): ...
Expand Down Expand Up @@ -144,7 +128,7 @@ class ZipFile:
def __init__(
self, file: Union[StrPath, IO[bytes]], mode: str = ..., compression: int = ..., allowZip64: bool = ...
) -> None: ...
def __enter__(self: _T) -> _T: ...
def __enter__(self: Self) -> Self: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
Expand Down

0 comments on commit 8a10746

Please sign in to comment.