Skip to content

Add missing methods methods in aiofiles #4734

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 8 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 3 additions & 3 deletions third_party/3/aiofiles/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
46 changes: 24 additions & 22 deletions third_party/3/aiofiles/threadpool/__init__.pyi
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,9 +22,11 @@ def open(
closefd: bool = ...,
opener: Optional[_Opener] = ...,
*,
loop: Optional[Any] = ...,
loop: Optional[AbstractEventLoop] = ...,
executor: Optional[Any] = ...,
) -> AiofilesContextManager[None, None, AsyncTextIOWrapper]: ...
) -> AiofilesContextManager[None, None, AsyncTextIOWrapper[_OpenFile]]: ...

# Unbuffered binary: returns a FileIO
@overload
def open(
file: _OpenFile,
Expand All @@ -33,37 +38,37 @@ def open(
closefd: bool = ...,
opener: Optional[_Opener] = ...,
*,
loop: Optional[Any] = ...,
loop: Optional[AbstractEventLoop] = ...,
executor: Optional[Any] = ...,
) -> AiofilesContextManager[None, None, AsyncFileIO]: ...
) -> AiofilesContextManager[None, None, AsyncFileIO[_OpenFile]]: ...

# Buffered binary reading/updating: AsyncBufferedReader
@overload
def open(
file: _OpenFile,
mode: OpenBinaryModeWriting,
mode: Union[OpenBinaryModeReading, OpenBinaryModeUpdating],
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
*,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove these arguments from some overloads?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's a mistake. Pushed a fix.

loop: Optional[Any] = ...,
executor: Optional[Any] = ...,
) -> AiofilesContextManager[None, None, AsyncBufferedIOBase]: ...
) -> AiofilesContextManager[None, None, AsyncBufferedReader[_OpenFile]]: ...

# Buffered binary writing: AsyncBufferedIOBase
@overload
def open(
file: _OpenFile,
mode: Union[OpenBinaryModeReading, OpenBinaryModeUpdating],
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, AsyncBufferedReader]: ...
) -> AiofilesContextManager[None, None, AsyncBufferedIOBase[_OpenFile]]: ...

# Buffering cannot be determined: fall back to _UnknownAsyncBinaryIO
@overload
def open(
file: _OpenFile,
Expand All @@ -74,7 +79,4 @@ def open(
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
*,
loop: Optional[Any] = ...,
executor: Optional[Any] = ...,
) -> AiofilesContextManager[None, None, AsyncBase[bytes]]: ...
) -> AiofilesContextManager[None, None, _UnknownAsyncBinaryIO[_OpenFile]]: ...
44 changes: 41 additions & 3 deletions third_party/3/aiofiles/threadpool/binary.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
from _typeshed import AnyPath, ReadableBuffer, WriteableBuffer
from io import FileIO
from typing import Generic, Iterable, List, Optional, TypeVar, Union

from ..base import AsyncBase

class AsyncBufferedIOBase(AsyncBase[bytes]): ...
class AsyncBufferedReader(AsyncBufferedIOBase): ...
class AsyncFileIO(AsyncBase[bytes]): ...
_FileName = TypeVar("_FileName", bound=Union[AnyPath, int])

class _UnknownAsyncBinaryIO(AsyncBase[bytes], Generic[_FileName]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure making this generic over _Filename is worth it; it's slightly more precise for the .filename property, but now forces everyone who uses one of these classes as a type annotation to add a type argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now forces everyone who uses one of these classes as a type annotation to add a type argument.

Is it true? I use pyright and it doesn't complain about not setting a generic type.

async def(file: AsyncTextIOWrapper):
    name = file.name
    # name is auto-detected to be of type: str | bytes | _PathLike[str] | _PathLike[bytes] | int

Of course, if other type checkers complain, I can annotate name property as -> Union[AnyPath, int].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy does this if you use the --disallow-any-generics option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, removed that.

async def close(self) -> None: ...
async def flush(self) -> None: ...
async def isatty(self) -> bool: ...
async def read(self, __size: int = ...) -> Optional[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) -> Optional[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) -> _FileName: ...

class AsyncBufferedIOBase(_UnknownAsyncBinaryIO[_FileName]):
async def read1(self, __size: int = ...) -> bytes: ...
def detach(self) -> FileIO: ...
@property
def raw(self) -> FileIO: ...

class AsyncBufferedReader(AsyncBufferedIOBase[_FileName]):
async def peek(self, __size: int = ...) -> bytes: ...

class AsyncFileIO(_UnknownAsyncBinaryIO[_FileName]):
async def readall(self) -> bytes: ...
39 changes: 38 additions & 1 deletion third_party/3/aiofiles/threadpool/text.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
from _typeshed import AnyPath
from typing import BinaryIO, Generic, Iterable, List, Optional, Tuple, TypeVar, Union

from ..base import AsyncBase

class AsyncTextIOWrapper(AsyncBase[str]): ...
_FileName = TypeVar("_FileName", bound=Union[AnyPath, int])

class AsyncTextIOWrapper(AsyncBase[str], Generic[_FileName]):
async def close(self) -> None: ...
async def flush(self) -> None: ...
async def isatty(self) -> bool: ...
async def read(self, __size: int = ...) -> str: ...
async def readline(self, __size: Optional[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) -> Optional[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) -> _FileName: ...
@property
def mode(self) -> str: ...