-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Conversation
Diff from mypy_primer, showing the effect of this PR on open source code: - starlette/responses.py:320: error: "AsyncBufferedReader" has no attribute "read"
+ starlette/responses.py:321: error: Argument 1 to "len" has incompatible type "Optional[bytes]"; expected "Sized"
|
buffering: Literal[-1, 1] = ..., | ||
encoding: None = ..., | ||
errors: None = ..., | ||
newline: None = ..., | ||
closefd: bool = ..., | ||
opener: Optional[_Opener] = ..., | ||
*, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
class AsyncFileIO(AsyncBase[bytes]): ... | ||
_FileName = TypeVar("_FileName", bound=Union[AnyPath, int]) | ||
|
||
class _UnknownAsyncBinaryIO(AsyncBase[bytes], Generic[_FileName]): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, removed that.
Diff from mypy_primer, showing the effect of this PR on open source code: - starlette/responses.py:320: error: "AsyncBufferedReader" has no attribute "read"
+ starlette/responses.py:321: error: Argument 1 to "len" has incompatible type "Optional[bytes]"; expected "Sized"
|
Diff from mypy_primer, showing the effect of this PR on open source code: - starlette/responses.py:320: error: "AsyncBufferedReader" has no attribute "read"
|
This will run mypy_primer on mypy PRs. Changes on the open source corpus are reported as comments on the PR. We integrated this into typeshed CI; you can see examples here: python/typeshed#3183 python/typeshed#4734 This might be a little slow. On typeshed this runs in 10 minutes, but it's using a mypyc compiled wheel. It looks like it takes three minutes to compile a MYPYC_OPT_LEVEL=0 wheel in our CI currently (for a ~2x speedup), so that's probably worth it.
This will run mypy_primer on mypy PRs. Changes on the open source corpus are reported as comments on the PR. We integrated this into typeshed CI; you can see examples here: python/typeshed#3183 python/typeshed#4734 This might be a little slow. On typeshed this runs in 10 minutes, but it's using a mypyc compiled wheel. It looks like it takes three minutes to compile a MYPYC_OPT_LEVEL=0 wheel in our CI currently (for a ~2x speedup), so that's probably worth it. Co-authored-by: hauntsaninja <>
This PR adds multiple missing methods in the
aiofiles
package.Objects returned by
aiofiles.open
have asynchronous methods corresponding to synchronous methods on usual file-like objects.aiofiles
"defines" those methods with a decorator directly on AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO and AsyncTextIOWrapper classes, so I just copied function signatures from the built-inio
module stubs to those classes. However, it's not always possible to determine thebuffering
attribute, so I had to create a fake_UnknownAsyncBinaryIO
class, which is an intersection of buffered and unbuffered file-like objects.I also made said async classes generic to create a proper signature for the
name
property.The
aiofiles._os
doesn't exist, insteadaiofiles.os
is available - I renamed the corresponding stub file.Fixes #4714.