Skip to content

Commit 40b44a9

Browse files
authored
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
1 parent 0e002f4 commit 40b44a9

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed

third_party/3/aiofiles/base.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class AsyncBase(Generic[_T]):
1212
async def __anext__(self) -> _T: ...
1313

1414
class AiofilesContextManager(Generic[_T_co, _T_contra, _V_co]):
15-
def __init__(self, __coro: Coroutine[_T_co, _T_contra, _V_co]) -> None: ...
16-
def send(self, __value: _T_contra) -> _T_co: ...
15+
def __init__(self, coro: Coroutine[_T_co, _T_contra, _V_co]) -> None: ...
16+
def send(self, value: _T_contra) -> _T_co: ...
1717
def throw(
18-
self, __typ: Type[BaseException], __val: Union[BaseException, object] = ..., tb: Optional[TracebackType] = ...
18+
self, typ: Type[BaseException], val: Union[BaseException, object] = ..., tb: Optional[TracebackType] = ...
1919
) -> _T_co: ...
2020
def close(self) -> None: ...
2121
@property
File renamed without changes.
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from _typeshed import AnyPath, OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode
2-
from typing import Any, Callable, Optional, Union, overload
2+
from asyncio import AbstractEventLoop
3+
from typing import Any, Callable, Optional, TypeVar, Union, overload
34
from typing_extensions import Literal
45

5-
from ..base import AiofilesContextManager, AsyncBase
6-
from .binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO
6+
from ..base import AiofilesContextManager
7+
from .binary import AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO, _UnknownAsyncBinaryIO
78
from .text import AsyncTextIOWrapper
89

9-
_OpenFile = Union[AnyPath, int]
10+
_OpenFile = TypeVar("_OpenFile", bound=Union[AnyPath, int])
1011
_Opener = Callable[[str, int], int]
12+
13+
# Text mode: always returns AsyncTextIOWrapper
1114
@overload
1215
def open(
1316
file: _OpenFile,
@@ -19,9 +22,11 @@ def open(
1922
closefd: bool = ...,
2023
opener: Optional[_Opener] = ...,
2124
*,
22-
loop: Optional[Any] = ...,
25+
loop: Optional[AbstractEventLoop] = ...,
2326
executor: Optional[Any] = ...,
2427
) -> AiofilesContextManager[None, None, AsyncTextIOWrapper]: ...
28+
29+
# Unbuffered binary: returns a FileIO
2530
@overload
2631
def open(
2732
file: _OpenFile,
@@ -33,37 +38,43 @@ def open(
3338
closefd: bool = ...,
3439
opener: Optional[_Opener] = ...,
3540
*,
36-
loop: Optional[Any] = ...,
41+
loop: Optional[AbstractEventLoop] = ...,
3742
executor: Optional[Any] = ...,
3843
) -> AiofilesContextManager[None, None, AsyncFileIO]: ...
44+
45+
# Buffered binary reading/updating: AsyncBufferedReader
3946
@overload
4047
def open(
4148
file: _OpenFile,
42-
mode: OpenBinaryModeWriting,
49+
mode: Union[OpenBinaryModeReading, OpenBinaryModeUpdating],
4350
buffering: Literal[-1, 1] = ...,
4451
encoding: None = ...,
4552
errors: None = ...,
4653
newline: None = ...,
4754
closefd: bool = ...,
4855
opener: Optional[_Opener] = ...,
4956
*,
50-
loop: Optional[Any] = ...,
57+
loop: Optional[AbstractEventLoop] = ...,
5158
executor: Optional[Any] = ...,
52-
) -> AiofilesContextManager[None, None, AsyncBufferedIOBase]: ...
59+
) -> AiofilesContextManager[None, None, AsyncBufferedReader]: ...
60+
61+
# Buffered binary writing: AsyncBufferedIOBase
5362
@overload
5463
def open(
5564
file: _OpenFile,
56-
mode: Union[OpenBinaryModeReading, OpenBinaryModeUpdating],
65+
mode: OpenBinaryModeWriting,
5766
buffering: Literal[-1, 1] = ...,
5867
encoding: None = ...,
5968
errors: None = ...,
6069
newline: None = ...,
6170
closefd: bool = ...,
6271
opener: Optional[_Opener] = ...,
6372
*,
64-
loop: Optional[Any] = ...,
73+
loop: Optional[AbstractEventLoop] = ...,
6574
executor: Optional[Any] = ...,
66-
) -> AiofilesContextManager[None, None, AsyncBufferedReader]: ...
75+
) -> AiofilesContextManager[None, None, AsyncBufferedIOBase]: ...
76+
77+
# Buffering cannot be determined: fall back to _UnknownAsyncBinaryIO
6778
@overload
6879
def open(
6980
file: _OpenFile,
@@ -75,6 +86,6 @@ def open(
7586
closefd: bool = ...,
7687
opener: Optional[_Opener] = ...,
7788
*,
78-
loop: Optional[Any] = ...,
89+
loop: Optional[AbstractEventLoop] = ...,
7990
executor: Optional[Any] = ...,
80-
) -> AiofilesContextManager[None, None, AsyncBase[bytes]]: ...
91+
) -> AiofilesContextManager[None, None, _UnknownAsyncBinaryIO]: ...
Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1+
from _typeshed import AnyPath, ReadableBuffer, WriteableBuffer
2+
from io import FileIO
3+
from typing import Iterable, List, Optional, Union
4+
15
from ..base import AsyncBase
26

3-
class AsyncBufferedIOBase(AsyncBase[bytes]): ...
4-
class AsyncBufferedReader(AsyncBufferedIOBase): ...
5-
class AsyncFileIO(AsyncBase[bytes]): ...
7+
class _UnknownAsyncBinaryIO(AsyncBase[bytes]):
8+
async def close(self) -> None: ...
9+
async def flush(self) -> None: ...
10+
async def isatty(self) -> bool: ...
11+
async def read(self, __size: int = ...) -> bytes: ...
12+
async def readinto(self, __buffer: WriteableBuffer) -> Optional[int]: ...
13+
async def readline(self, __size: Optional[int] = ...) -> bytes: ...
14+
async def readlines(self, __hint: int = ...) -> List[bytes]: ...
15+
async def seek(self, __offset: int, __whence: int = ...) -> int: ...
16+
async def seekable(self) -> bool: ...
17+
async def tell(self) -> int: ...
18+
async def truncate(self, __size: Optional[int] = ...) -> int: ...
19+
async def writable(self) -> bool: ...
20+
async def write(self, __b: ReadableBuffer) -> int: ...
21+
async def writelines(self, __lines: Iterable[ReadableBuffer]) -> None: ...
22+
def fileno(self) -> int: ...
23+
def readable(self) -> bool: ...
24+
@property
25+
def closed(self) -> bool: ...
26+
@property
27+
def mode(self) -> str: ...
28+
@property
29+
def name(self) -> Union[AnyPath, int]: ...
30+
31+
class AsyncBufferedIOBase(_UnknownAsyncBinaryIO):
32+
async def read1(self, __size: int = ...) -> bytes: ...
33+
def detach(self) -> FileIO: ...
34+
@property
35+
def raw(self) -> FileIO: ...
36+
37+
class AsyncBufferedReader(AsyncBufferedIOBase):
38+
async def peek(self, __size: int = ...) -> bytes: ...
39+
40+
class AsyncFileIO(_UnknownAsyncBinaryIO):
41+
async def readall(self) -> bytes: ...
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
from _typeshed import AnyPath
2+
from typing import BinaryIO, Iterable, List, Optional, Tuple, Union
3+
14
from ..base import AsyncBase
25

3-
class AsyncTextIOWrapper(AsyncBase[str]): ...
6+
class AsyncTextIOWrapper(AsyncBase[str]):
7+
async def close(self) -> None: ...
8+
async def flush(self) -> None: ...
9+
async def isatty(self) -> bool: ...
10+
async def read(self, __size: Optional[int] = ...) -> str: ...
11+
async def readline(self, __size: int = ...) -> str: ...
12+
async def readlines(self, __hint: int = ...) -> List[str]: ...
13+
async def seek(self, __offset: int, __whence: int = ...) -> int: ...
14+
async def seekable(self) -> bool: ...
15+
async def tell(self) -> int: ...
16+
async def truncate(self, __size: Optional[int] = ...) -> int: ...
17+
async def writable(self) -> bool: ...
18+
async def write(self, __b: str) -> int: ...
19+
async def writelines(self, __lines: Iterable[str]) -> None: ...
20+
def detach(self) -> BinaryIO: ...
21+
def fileno(self) -> int: ...
22+
def readable(self) -> bool: ...
23+
@property
24+
def buffer(self) -> BinaryIO: ...
25+
@property
26+
def closed(self) -> bool: ...
27+
@property
28+
def encoding(self) -> str: ...
29+
@property
30+
def errors(self) -> Optional[str]: ...
31+
@property
32+
def line_buffering(self) -> bool: ...
33+
@property
34+
def newlines(self) -> Union[str, Tuple[str, ...], None]: ...
35+
@property
36+
def name(self) -> Union[AnyPath, int]: ...
37+
@property
38+
def mode(self) -> str: ...

0 commit comments

Comments
 (0)