From 40b44a9bf19406ccc68b9f1fc0510b994cc38241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kuranowski?= Date: Mon, 2 Nov 2020 01:08:14 +0100 Subject: [PATCH] Add missing methods methods in aiofiles (#4734) * fix aiofiles stubs * switch io.BinaryIO to typing.BinaryIO * swap typing.Literal to typing_extensions.Literal * fix overload overlap in open function * Fix filename generics * add missing loop and executor arguments to aiofiles.open * Remove generics from async files * Fix async file-like read, readline and write signatures --- third_party/3/aiofiles/base.pyi | 6 +- third_party/3/aiofiles/{_os.pyi => os.pyi} | 0 .../3/aiofiles/threadpool/__init__.pyi | 57 +++++++++++-------- third_party/3/aiofiles/threadpool/binary.pyi | 42 +++++++++++++- third_party/3/aiofiles/threadpool/text.pyi | 37 +++++++++++- 5 files changed, 112 insertions(+), 30 deletions(-) rename third_party/3/aiofiles/{_os.pyi => os.pyi} (100%) diff --git a/third_party/3/aiofiles/base.pyi b/third_party/3/aiofiles/base.pyi index 13c76ed7e..0f5f99a2e 100644 --- a/third_party/3/aiofiles/base.pyi +++ b/third_party/3/aiofiles/base.pyi @@ -12,10 +12,10 @@ class AsyncBase(Generic[_T]): async def __anext__(self) -> _T: ... class AiofilesContextManager(Generic[_T_co, _T_contra, _V_co]): - def __init__(self, __coro: Coroutine[_T_co, _T_contra, _V_co]) -> None: ... - def send(self, __value: _T_contra) -> _T_co: ... + def __init__(self, coro: Coroutine[_T_co, _T_contra, _V_co]) -> None: ... + def send(self, value: _T_contra) -> _T_co: ... def throw( - self, __typ: Type[BaseException], __val: Union[BaseException, object] = ..., tb: Optional[TracebackType] = ... + self, typ: Type[BaseException], val: Union[BaseException, object] = ..., tb: Optional[TracebackType] = ... ) -> _T_co: ... def close(self) -> None: ... @property diff --git a/third_party/3/aiofiles/_os.pyi b/third_party/3/aiofiles/os.pyi similarity index 100% rename from third_party/3/aiofiles/_os.pyi rename to third_party/3/aiofiles/os.pyi diff --git a/third_party/3/aiofiles/threadpool/__init__.pyi b/third_party/3/aiofiles/threadpool/__init__.pyi index 840f9ce48..bde6ad8a3 100644 --- a/third_party/3/aiofiles/threadpool/__init__.pyi +++ b/third_party/3/aiofiles/threadpool/__init__.pyi @@ -1,13 +1,16 @@ from _typeshed import AnyPath, OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode -from typing import Any, Callable, Optional, Union, overload +from asyncio import AbstractEventLoop +from typing import Any, Callable, Optional, TypeVar, Union, overload from typing_extensions import Literal -from ..base import AiofilesContextManager, AsyncBase -from .binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO +from ..base import AiofilesContextManager +from .binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO, _UnknownAsyncBinaryIO from .text import AsyncTextIOWrapper -_OpenFile = Union[AnyPath, int] +_OpenFile = TypeVar("_OpenFile", bound=Union[AnyPath, int]) _Opener = Callable[[str, int], int] + +# Text mode: always returns AsyncTextIOWrapper @overload def open( file: _OpenFile, @@ -19,9 +22,11 @@ def open( closefd: bool = ..., opener: Optional[_Opener] = ..., *, - loop: Optional[Any] = ..., + loop: Optional[AbstractEventLoop] = ..., executor: Optional[Any] = ..., ) -> AiofilesContextManager[None, None, AsyncTextIOWrapper]: ... + +# Unbuffered binary: returns a FileIO @overload def open( file: _OpenFile, @@ -33,23 +38,11 @@ def open( closefd: bool = ..., opener: Optional[_Opener] = ..., *, - loop: Optional[Any] = ..., + loop: Optional[AbstractEventLoop] = ..., executor: Optional[Any] = ..., ) -> AiofilesContextManager[None, None, AsyncFileIO]: ... -@overload -def open( - file: _OpenFile, - mode: OpenBinaryModeWriting, - buffering: Literal[-1, 1] = ..., - encoding: None = ..., - errors: None = ..., - newline: None = ..., - closefd: bool = ..., - opener: Optional[_Opener] = ..., - *, - loop: Optional[Any] = ..., - executor: Optional[Any] = ..., -) -> AiofilesContextManager[None, None, AsyncBufferedIOBase]: ... + +# Buffered binary reading/updating: AsyncBufferedReader @overload def open( file: _OpenFile, @@ -61,9 +54,27 @@ def open( closefd: bool = ..., opener: Optional[_Opener] = ..., *, - loop: Optional[Any] = ..., + loop: Optional[AbstractEventLoop] = ..., executor: Optional[Any] = ..., ) -> AiofilesContextManager[None, None, AsyncBufferedReader]: ... + +# Buffered binary writing: AsyncBufferedIOBase +@overload +def open( + file: _OpenFile, + mode: OpenBinaryModeWriting, + buffering: Literal[-1, 1] = ..., + encoding: None = ..., + errors: None = ..., + newline: None = ..., + closefd: bool = ..., + opener: Optional[_Opener] = ..., + *, + loop: Optional[AbstractEventLoop] = ..., + executor: Optional[Any] = ..., +) -> AiofilesContextManager[None, None, AsyncBufferedIOBase]: ... + +# Buffering cannot be determined: fall back to _UnknownAsyncBinaryIO @overload def open( file: _OpenFile, @@ -75,6 +86,6 @@ def open( closefd: bool = ..., opener: Optional[_Opener] = ..., *, - loop: Optional[Any] = ..., + loop: Optional[AbstractEventLoop] = ..., executor: Optional[Any] = ..., -) -> AiofilesContextManager[None, None, AsyncBase[bytes]]: ... +) -> AiofilesContextManager[None, None, _UnknownAsyncBinaryIO]: ... diff --git a/third_party/3/aiofiles/threadpool/binary.pyi b/third_party/3/aiofiles/threadpool/binary.pyi index 5c51c064a..ecfd4ed12 100644 --- a/third_party/3/aiofiles/threadpool/binary.pyi +++ b/third_party/3/aiofiles/threadpool/binary.pyi @@ -1,5 +1,41 @@ +from _typeshed import AnyPath, ReadableBuffer, WriteableBuffer +from io import FileIO +from typing import Iterable, List, Optional, Union + from ..base import AsyncBase -class AsyncBufferedIOBase(AsyncBase[bytes]): ... -class AsyncBufferedReader(AsyncBufferedIOBase): ... -class AsyncFileIO(AsyncBase[bytes]): ... +class _UnknownAsyncBinaryIO(AsyncBase[bytes]): + async def close(self) -> None: ... + async def flush(self) -> None: ... + async def isatty(self) -> bool: ... + async def read(self, __size: int = ...) -> bytes: ... + async def readinto(self, __buffer: WriteableBuffer) -> Optional[int]: ... + async def readline(self, __size: Optional[int] = ...) -> bytes: ... + async def readlines(self, __hint: int = ...) -> List[bytes]: ... + async def seek(self, __offset: int, __whence: int = ...) -> int: ... + async def seekable(self) -> bool: ... + async def tell(self) -> int: ... + async def truncate(self, __size: Optional[int] = ...) -> int: ... + async def writable(self) -> bool: ... + async def write(self, __b: ReadableBuffer) -> int: ... + async def writelines(self, __lines: Iterable[ReadableBuffer]) -> None: ... + def fileno(self) -> int: ... + def readable(self) -> bool: ... + @property + def closed(self) -> bool: ... + @property + def mode(self) -> str: ... + @property + def name(self) -> Union[AnyPath, int]: ... + +class AsyncBufferedIOBase(_UnknownAsyncBinaryIO): + async def read1(self, __size: int = ...) -> bytes: ... + def detach(self) -> FileIO: ... + @property + def raw(self) -> FileIO: ... + +class AsyncBufferedReader(AsyncBufferedIOBase): + async def peek(self, __size: int = ...) -> bytes: ... + +class AsyncFileIO(_UnknownAsyncBinaryIO): + async def readall(self) -> bytes: ... diff --git a/third_party/3/aiofiles/threadpool/text.pyi b/third_party/3/aiofiles/threadpool/text.pyi index 7662768f3..39588f8a3 100644 --- a/third_party/3/aiofiles/threadpool/text.pyi +++ b/third_party/3/aiofiles/threadpool/text.pyi @@ -1,3 +1,38 @@ +from _typeshed import AnyPath +from typing import BinaryIO, Iterable, List, Optional, Tuple, Union + from ..base import AsyncBase -class AsyncTextIOWrapper(AsyncBase[str]): ... +class AsyncTextIOWrapper(AsyncBase[str]): + async def close(self) -> None: ... + async def flush(self) -> None: ... + async def isatty(self) -> bool: ... + async def read(self, __size: Optional[int] = ...) -> str: ... + async def readline(self, __size: int = ...) -> str: ... + async def readlines(self, __hint: int = ...) -> List[str]: ... + async def seek(self, __offset: int, __whence: int = ...) -> int: ... + async def seekable(self) -> bool: ... + async def tell(self) -> int: ... + async def truncate(self, __size: Optional[int] = ...) -> int: ... + async def writable(self) -> bool: ... + async def write(self, __b: str) -> int: ... + async def writelines(self, __lines: Iterable[str]) -> None: ... + def detach(self) -> BinaryIO: ... + def fileno(self) -> int: ... + def readable(self) -> bool: ... + @property + def buffer(self) -> BinaryIO: ... + @property + def closed(self) -> bool: ... + @property + def encoding(self) -> str: ... + @property + def errors(self) -> Optional[str]: ... + @property + def line_buffering(self) -> bool: ... + @property + def newlines(self) -> Union[str, Tuple[str, ...], None]: ... + @property + def name(self) -> Union[AnyPath, int]: ... + @property + def mode(self) -> str: ...