From bbd8c96e346591b113e337e52d5400c6ed426441 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sat, 29 Aug 2020 16:45:36 -0700 Subject: [PATCH] Added some missing types from various stdlib stubs (#4466) --- stdlib/2and3/pickle.pyi | 4 +-- stdlib/2and3/socket.pyi | 10 +++---- stdlib/3/_compression.pyi | 29 ++++++++++++------- stdlib/3/_thread.pyi | 2 +- stdlib/3/asyncio/trsock.pyi | 12 +++++--- stdlib/3/asyncio/windows_utils.pyi | 2 +- stdlib/3/multiprocessing/dummy/__init__.pyi | 19 ++++++++---- stdlib/3/multiprocessing/dummy/connection.pyi | 18 ++++++++---- stdlib/3/runpy.pyi | 20 +++++++------ stdlib/3/typing.pyi | 2 +- 10 files changed, 73 insertions(+), 45 deletions(-) diff --git a/stdlib/2and3/pickle.pyi b/stdlib/2and3/pickle.pyi index 1c2fcca9c..5ca023db8 100644 --- a/stdlib/2and3/pickle.pyi +++ b/stdlib/2and3/pickle.pyi @@ -56,8 +56,8 @@ _reducedtype = Union[ str, Tuple[Callable[..., Any], Tuple[Any, ...]], Tuple[Callable[..., Any], Tuple[Any, ...], Any], - Tuple[Callable[..., Any], Tuple[Any, ...], Any, Optional[Iterator]], - Tuple[Callable[..., Any], Tuple[Any, ...], Any, Optional[Iterator], Optional[Iterator]], + Tuple[Callable[..., Any], Tuple[Any, ...], Any, Optional[Iterator[Any]]], + Tuple[Callable[..., Any], Tuple[Any, ...], Any, Optional[Iterator[Any]], Optional[Iterator[Any]]], ] class Pickler: diff --git a/stdlib/2and3/socket.pyi b/stdlib/2and3/socket.pyi index c48d433b1..7ab6a75a0 100644 --- a/stdlib/2and3/socket.pyi +++ b/stdlib/2and3/socket.pyi @@ -15,7 +15,7 @@ CPython C source: https://github.com/python/cpython/blob/master/Modules/socketmo # Ron Murawski # adapted for Python 2.7 by Michal Pokorny import sys -from typing import Any, BinaryIO, Iterable, List, Optional, Text, TextIO, Tuple, TypeVar, Union, overload +from typing import Any, BinaryIO, Iterable, List, Optional, Sequence, Text, TextIO, Tuple, TypeVar, Union, overload from typing_extensions import Literal # ----- Constants ----- @@ -713,11 +713,11 @@ class socket: self, __buffers: Iterable[bytes], __ancdata: Iterable[_CMSG] = ..., __flags: int = ..., __address: _Address = ... ) -> int: ... if sys.platform == "linux" and sys.version_info >= (3, 6): - # TODO add the parameter types for sendmsg_afalg - def sendmsg_afalg(self, msg=..., *, op, iv=..., assoclen=..., flags=...) -> int: ... + def sendmsg_afalg( + self, msg: Iterable[bytes] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ... + ) -> int: ... if sys.version_info >= (3,): - # TODO determine legal types for file parameter - def sendfile(self, file, offset: int = ..., count: Optional[int] = ...) -> int: ... + def sendfile(self, file: BinaryIO, offset: int = ..., count: Optional[int] = ...) -> int: ... def set_inheritable(self, inheritable: bool) -> None: ... def setblocking(self, flag: bool) -> None: ... def settimeout(self, value: Optional[float]) -> None: ... diff --git a/stdlib/3/_compression.pyi b/stdlib/3/_compression.pyi index 0fec600fd..ac44d6d65 100644 --- a/stdlib/3/_compression.pyi +++ b/stdlib/3/_compression.pyi @@ -1,16 +1,23 @@ -import io -from typing import Any +from _typeshed import WriteableBuffer +from io import BufferedIOBase, RawIOBase +from typing import Any, Callable, Tuple, Type, Union BUFFER_SIZE: Any -class BaseStream(io.BufferedIOBase): ... +class BaseStream(BufferedIOBase): ... -class DecompressReader(io.RawIOBase): - def readable(self): ... - def __init__(self, fp, decomp_factory, trailing_error=..., **decomp_args): ... - def close(self): ... - def seekable(self): ... - def readinto(self, b): ... +class DecompressReader(RawIOBase): + def __init__( + self, + fp: RawIOBase, + decomp_factory: Callable[..., object], + trailing_error: Union[Type[Exception], Tuple[Type[Exception], ...]] = ..., + **decomp_args: Any, + ) -> None: ... + def readable(self) -> bool: ... + def close(self) -> None: ... + def seekable(self) -> bool: ... + def readinto(self, b: WriteableBuffer) -> int: ... def read(self, size: int = ...) -> bytes: ... - def seek(self, offset, whence=...): ... - def tell(self): ... + def seek(self, offset: int, whence: int = ...) -> int: ... + def tell(self) -> int: ... diff --git a/stdlib/3/_thread.pyi b/stdlib/3/_thread.pyi index a0dabcdae..ded307b03 100644 --- a/stdlib/3/_thread.pyi +++ b/stdlib/3/_thread.pyi @@ -36,5 +36,5 @@ if sys.version_info >= (3, 8): exc_value: Optional[BaseException] exc_traceback: Optional[TracebackType] thread: Optional[Thread] - def _ExceptHookArgs(args) -> ExceptHookArgs: ... + def _ExceptHookArgs(args: Any) -> ExceptHookArgs: ... _excepthook: Callable[[ExceptHookArgs], Any] diff --git a/stdlib/3/asyncio/trsock.pyi b/stdlib/3/asyncio/trsock.pyi index 2b7aad9d6..0c6a7b332 100644 --- a/stdlib/3/asyncio/trsock.pyi +++ b/stdlib/3/asyncio/trsock.pyi @@ -1,7 +1,7 @@ import socket import sys from types import TracebackType -from typing import Any, BinaryIO, Iterable, List, NoReturn, Optional, Tuple, Type, Union, overload +from typing import Any, BinaryIO, Iterable, List, NoReturn, Optional, Sequence, Tuple, Type, Union, overload if sys.version_info >= (3, 8): # These are based in socket, maybe move them out into _typeshed.pyi or such @@ -44,13 +44,17 @@ if sys.version_info >= (3, 8): def ioctl(self, control: int, option: Union[int, Tuple[int, int, int], bool]) -> NoReturn: ... def listen(self, __backlog: int = ...) -> None: ... def makefile(self) -> BinaryIO: ... - def sendfile(self, file, offset: int = ..., count: Optional[int] = ...) -> int: ... + def sendfile(self, file: BinaryIO, offset: int = ..., count: Optional[int] = ...) -> int: ... def close(self) -> None: ... def detach(self) -> int: ... if sys.platform == "linux": - def sendmsg_afalg(self, msg=..., *, op, iv=..., assoclen=..., flags=...) -> int: ... + def sendmsg_afalg( + self, msg: Iterable[bytes] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ... + ) -> int: ... else: - def sendmsg_afalg(self, msg=..., *, op, iv=..., assoclen=..., flags=...) -> NoReturn: ... + def sendmsg_afalg( + self, msg: Iterable[bytes] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ... + ) -> NoReturn: ... def sendmsg( self, __buffers: Iterable[bytes], __ancdata: Iterable[_CMSG] = ..., __flags: int = ..., __address: _Address = ... ) -> int: ... diff --git a/stdlib/3/asyncio/windows_utils.pyi b/stdlib/3/asyncio/windows_utils.pyi index b743d5ccb..459bb7845 100644 --- a/stdlib/3/asyncio/windows_utils.pyi +++ b/stdlib/3/asyncio/windows_utils.pyi @@ -3,7 +3,7 @@ from types import TracebackType from typing import Callable, Optional, Protocol, Tuple, Type class _WarnFunction(Protocol): - def __call__(self, message: str, category: Type[Warning], source: PipeHandle): ... + def __call__(self, message: str, category: Type[Warning], source: PipeHandle) -> None: ... BUFSIZE: int PIPE: int diff --git a/stdlib/3/multiprocessing/dummy/__init__.pyi b/stdlib/3/multiprocessing/dummy/__init__.pyi index 94c43c1fd..0089defc0 100644 --- a/stdlib/3/multiprocessing/dummy/__init__.pyi +++ b/stdlib/3/multiprocessing/dummy/__init__.pyi @@ -2,7 +2,7 @@ import array import threading import weakref from queue import Queue as Queue -from typing import Any, List, Optional +from typing import Any, Callable, Iterable, List, Mapping, Optional, Sequence JoinableQueue = Queue Barrier = threading.Barrier @@ -19,7 +19,14 @@ class DummyProcess(threading.Thread): _pid: None _start_called: int exitcode: Optional[int] - def __init__(self, group=..., target=..., name=..., args=..., kwargs=...) -> None: ... + def __init__( + self, + group: Any = ..., + target: Optional[Callable[..., Any]] = ..., + name: Optional[str] = ..., + args: Iterable[Any] = ..., + kwargs: Mapping[str, Any] = ..., + ) -> None: ... Process = DummyProcess @@ -32,11 +39,13 @@ class Value: _typecode: Any _value: Any value: Any - def __init__(self, typecode, value, lock=...) -> None: ... + def __init__(self, typecode: Any, value: Any, lock: Any = ...) -> None: ... -def Array(typecode, sequence, lock=...) -> array.array[Any]: ... +def Array(typecode: Any, sequence: Sequence[Any], lock: Any = ...) -> array.array[Any]: ... def Manager() -> Any: ... -def Pool(processes=..., initializer=..., initargs=...) -> Any: ... +def Pool( + processes: Optional[int] = ..., initializer: Optional[Callable[..., Any]] = ..., initargs: Iterable[Any] = ... +) -> Any: ... def active_children() -> List[Any]: ... def current_process() -> threading.Thread: ... def freeze_support() -> None: ... diff --git a/stdlib/3/multiprocessing/dummy/connection.pyi b/stdlib/3/multiprocessing/dummy/connection.pyi index 599c86d32..4f9f71695 100644 --- a/stdlib/3/multiprocessing/dummy/connection.pyi +++ b/stdlib/3/multiprocessing/dummy/connection.pyi @@ -1,10 +1,12 @@ from queue import Queue -from typing import Any, List, Optional, Tuple, TypeVar +from types import TracebackType +from typing import Any, List, Optional, Tuple, Type, TypeVar, Union families: List[None] _TConnection = TypeVar("_TConnection", bound=Connection) _TListener = TypeVar("_TListener", bound=Listener) +_Address = Union[str, Tuple[str, int]] class Connection(object): _in: Any @@ -14,8 +16,10 @@ class Connection(object): send: Any send_bytes: Any def __enter__(self: _TConnection) -> _TConnection: ... - def __exit__(self, exc_type, exc_value, exc_tb) -> None: ... - def __init__(self, _in, _out) -> None: ... + def __exit__( + self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] + ) -> None: ... + def __init__(self, _in: Any, _out: Any) -> None: ... def close(self) -> None: ... def poll(self, timeout: float = ...) -> bool: ... @@ -24,10 +28,12 @@ class Listener(object): @property def address(self) -> Optional[Queue[Any]]: ... def __enter__(self: _TListener) -> _TListener: ... - def __exit__(self, exc_type, exc_value, exc_tb) -> None: ... - def __init__(self, address=..., family=..., backlog=...) -> None: ... + def __exit__( + self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] + ) -> None: ... + def __init__(self, address: Optional[_Address] = ..., family: Optional[int] = ..., backlog: int = ...) -> None: ... def accept(self) -> Connection: ... def close(self) -> None: ... -def Client(address) -> Connection: ... +def Client(address: _Address) -> Connection: ... def Pipe(duplex: bool = ...) -> Tuple[Connection, Connection]: ... diff --git a/stdlib/3/runpy.pyi b/stdlib/3/runpy.pyi index c58da400f..e27849d9c 100644 --- a/stdlib/3/runpy.pyi +++ b/stdlib/3/runpy.pyi @@ -1,20 +1,22 @@ from types import ModuleType -from typing import Any, Dict, Optional +from typing import Any, Dict, Optional, TypeVar + +_T = TypeVar("_T") class _TempModule: mod_name: str = ... module: ModuleType = ... - def __init__(self, mod_name): ... - def __enter__(self): ... - def __exit__(self, *args): ... + def __init__(self, mod_name: str) -> None: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, *args: Any) -> None: ... class _ModifiedArgv0: value: Any = ... - def __init__(self, value): ... - def __enter__(self): ... - def __exit__(self, *args): ... + def __init__(self, value: Any) -> None: ... + def __enter__(self: _T) -> _T: ... + def __exit__(self, *args: Any) -> None: ... def run_module( mod_name: str, init_globals: Optional[Dict[str, Any]] = ..., run_name: Optional[str] = ..., alter_sys: bool = ... -): ... -def run_path(path_name: str, init_globals: Optional[Dict[str, Any]] = ..., run_name: str = ...): ... +) -> None: ... +def run_path(path_name: str, init_globals: Optional[Dict[str, Any]] = ..., run_name: str = ...) -> None: ... diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index f8e262927..a3f46e9b5 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -281,7 +281,7 @@ if sys.version_info >= (3, 6): # Implement Sized (but don't have it as a base class). @abstractmethod def __len__(self) -> int: ... - _Collection = Collection + _Collection = Collection[_T_co] else: @runtime_checkable class _Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):