Skip to content

Use _typeshed.Self with __enter__ #5723

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 3 commits into from
Jul 4, 2021
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
4 changes: 2 additions & 2 deletions stdlib/http/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/multiprocessing/connection.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
9 changes: 4 additions & 5 deletions stdlib/multiprocessing/dummy/connection.pyi
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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: ...
Expand All @@ -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: ...
Expand Down
3 changes: 2 additions & 1 deletion stdlib/multiprocessing/pool.pyi
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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__(
Expand Down
3 changes: 2 additions & 1 deletion stdlib/os/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from _typeshed import (
OpenBinaryModeUpdating,
OpenBinaryModeWriting,
OpenTextMode,
Self,
StrOrBytesPath,
StrPath,
)
Expand Down Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions stdlib/sqlite3/dbapi2.pyi
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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]):
Expand Down
15 changes: 12 additions & 3 deletions stdlib/unittest/case.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -14,6 +15,7 @@ from typing import (
Iterable,
List,
Mapping,
NamedTuple,
NoReturn,
Optional,
Pattern,
Expand Down Expand Up @@ -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: ...
Expand All @@ -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: ...
Expand All @@ -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]: ...
3 changes: 2 additions & 1 deletion stdlib/urllib/response.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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: ...
Expand Down
7 changes: 3 additions & 4 deletions stdlib/xml/dom/minidom.pyi
Original file line number Diff line number Diff line change
@@ -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=...): ...
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions stdlib/xmlrpc/client.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: ...
Expand Down