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

Add missing methods methods in aiofiles #4734

merged 8 commits into from
Nov 2, 2020

Conversation

MKuranowski
Copy link
Contributor

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-in io module stubs to those classes. However, it's not always possible to determine the buffering 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, instead aiofiles.os is available - I renamed the corresponding stub file.

Fixes #4714.

@github-actions
Copy link
Contributor

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] = ...,
*,
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.

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.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 1, 2020

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"

@github-actions
Copy link
Contributor

github-actions bot commented Nov 1, 2020

Diff from mypy_primer, showing the effect of this PR on open source code:

- starlette/responses.py:320: error: "AsyncBufferedReader" has no attribute "read"

@JelleZijlstra JelleZijlstra merged commit 40b44a9 into python:master Nov 2, 2020
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this pull request Nov 2, 2020
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.
JukkaL pushed a commit to python/mypy that referenced this pull request Nov 4, 2020
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 <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid aiofiles stubs
2 participants