FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add missing methods methods in aiofiles by MKuranowski · Pull Request #4734 · python/typeshed · GitHub

Repository navigation

Add missing methods methods in aiofiles - #4734

Merged
JelleZijlstra merged 8 commits into
python:masterfrom
MKuranowski:master
Nov 2, 2020
Merged

JelleZijlstra merged 8 commits into
python:masterfrom
MKuranowski:master

Conversation

Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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"

newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
*,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Why did you remove these arguments from some overloads?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ok, removed that.

github-actions Bot commented Nov 1, 2020

Copy link
Copy Markdown
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"

github-actions Bot commented Nov 1, 2020

Copy link
Copy Markdown
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"

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 <>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL