mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-06 12:14:27 +08:00
This pull request is a follow-up to https://github.com/python/mypy/issues/7214. In short, within that mypy issue, we found it would be helpful to determine between contextmanagers that can "swallow" exceptions vs ones that can't. This helps prevent some false positive when using flags that analyze control flow such as `--warn-unreachable`. To do this, Jelle proposed assuming that only contextmanagers where the `__exit__` returns `bool` are assumed to swallow exceptions. This unfortunately required the following typeshed changes: 1. The typing.IO, threading.Lock, and concurrent.futures.Executor were all modified so `__exit__` returns `Optional[None]` instead of None -- along with all of their subclasses. I believe these three types are meant to be subclassed, so I felt picking the more general type was correct. 2. There were also a few concrete types (e.g. see socketserver, subprocess, ftplib...) that I modified to return `None` -- I checked the source code, and these all seem to return None (and don't appear to be meant to be subclassable). 3. contextlib.suppress was changed to return bool. I also double-checked the unittest modules and modified a subset of those contextmanagers, leaving ones like `_AssertRaisesContext` alone.
100 lines
3.7 KiB
Python
100 lines
3.7 KiB
Python
# NB: SocketServer.pyi and socketserver.pyi must remain consistent!
|
|
# Stubs for socketserver
|
|
|
|
from typing import Any, BinaryIO, Optional, Tuple, Type, Text, Union
|
|
from socket import SocketType
|
|
import sys
|
|
import types
|
|
|
|
class BaseServer:
|
|
address_family: int
|
|
RequestHandlerClass: type
|
|
server_address: Tuple[str, int]
|
|
socket: SocketType
|
|
allow_reuse_address: bool
|
|
request_queue_size: int
|
|
socket_type: int
|
|
timeout: Optional[float]
|
|
def __init__(self, server_address: Any,
|
|
RequestHandlerClass: type) -> None: ...
|
|
def fileno(self) -> int: ...
|
|
def handle_request(self) -> None: ...
|
|
def serve_forever(self, poll_interval: float = ...) -> None: ...
|
|
def shutdown(self) -> None: ...
|
|
def server_close(self) -> None: ...
|
|
def finish_request(self, request: bytes,
|
|
client_address: Tuple[str, int]) -> None: ...
|
|
def get_request(self) -> None: ...
|
|
def handle_error(self, request: bytes,
|
|
client_address: Tuple[str, int]) -> None: ...
|
|
def handle_timeout(self) -> None: ...
|
|
def process_request(self, request: bytes,
|
|
client_address: Tuple[str, int]) -> None: ...
|
|
def server_activate(self) -> None: ...
|
|
def server_bind(self) -> None: ...
|
|
def verify_request(self, request: bytes,
|
|
client_address: Tuple[str, int]) -> bool: ...
|
|
if sys.version_info >= (3, 6):
|
|
def __enter__(self) -> BaseServer: ...
|
|
def __exit__(self, exc_type: Optional[Type[BaseException]],
|
|
exc_val: Optional[BaseException],
|
|
exc_tb: Optional[types.TracebackType]) -> None: ...
|
|
if sys.version_info >= (3, 3):
|
|
def service_actions(self) -> None: ...
|
|
|
|
class TCPServer(BaseServer):
|
|
def __init__(self, server_address: Tuple[str, int],
|
|
RequestHandlerClass: type,
|
|
bind_and_activate: bool = ...) -> None: ...
|
|
|
|
class UDPServer(BaseServer):
|
|
def __init__(self, server_address: Tuple[str, int],
|
|
RequestHandlerClass: type,
|
|
bind_and_activate: bool = ...) -> None: ...
|
|
|
|
if sys.platform != 'win32':
|
|
class UnixStreamServer(BaseServer):
|
|
def __init__(self, server_address: Union[Text, bytes],
|
|
RequestHandlerClass: type,
|
|
bind_and_activate: bool = ...) -> None: ...
|
|
|
|
class UnixDatagramServer(BaseServer):
|
|
def __init__(self, server_address: Union[Text, bytes],
|
|
RequestHandlerClass: type,
|
|
bind_and_activate: bool = ...) -> None: ...
|
|
|
|
class ForkingMixIn: ...
|
|
class ThreadingMixIn: ...
|
|
|
|
class ForkingTCPServer(ForkingMixIn, TCPServer): ...
|
|
class ForkingUDPServer(ForkingMixIn, UDPServer): ...
|
|
class ThreadingTCPServer(ThreadingMixIn, TCPServer): ...
|
|
class ThreadingUDPServer(ThreadingMixIn, UDPServer): ...
|
|
if sys.platform != 'win32':
|
|
class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer): ...
|
|
class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer): ...
|
|
|
|
|
|
class BaseRequestHandler:
|
|
# Those are technically of types, respectively:
|
|
# * Union[SocketType, Tuple[bytes, SocketType]]
|
|
# * Union[Tuple[str, int], str]
|
|
# But there are some concerns that having unions here would cause
|
|
# too much inconvenience to people using it (see
|
|
# https://github.com/python/typeshed/pull/384#issuecomment-234649696)
|
|
request: Any
|
|
client_address: Any
|
|
|
|
server: BaseServer
|
|
def setup(self) -> None: ...
|
|
def handle(self) -> None: ...
|
|
def finish(self) -> None: ...
|
|
|
|
class StreamRequestHandler(BaseRequestHandler):
|
|
rfile: BinaryIO
|
|
wfile: BinaryIO
|
|
|
|
class DatagramRequestHandler(BaseRequestHandler):
|
|
rfile: BinaryIO
|
|
wfile: BinaryIO
|