Skip to content
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

stream_with_context() might have missing type information #4052

Closed
hughsie opened this issue May 14, 2021 · 0 comments
Closed

stream_with_context() might have missing type information #4052

hughsie opened this issue May 14, 2021 · 0 comments
Labels
Milestone

Comments

@hughsie
Copy link

hughsie commented May 14, 2021

On the LVFS we have the following code:

def _read_file_chunks(fn: str, chunksz: int = 0x80000) -> Iterator[bytes]:
    with open(fn, "rb") as fd:
        while 1:
            buf = fd.read(chunksz)
            if buf:
                yield buf
            else:
                break

Which is passed into a Response to achieve downloading chunking like this:

return Response(
    stream_with_context(_read_file_chunks(fullpath)),
    headers={
        "Content-Disposition": "attachment; filename={}".format(filename),
        "Content-Length": str(os.path.getsize(fullpath)),
    },
)

I think Iterator[bytes] is sane, according to https://mypy.readthedocs.io/en/stable/kinds_of_types.html#generators but mypy reports:

error: Argument 1 to "stream_with_context" has incompatible type "Iterator[bytes]"; expected "Union[Generator[Any, Any, Any], Callable[..., Any]]"

Perhaps stream_with_context() needs the additional Iterator[bytes] added to the Union[]?

Environment:

  • Python version: 3.9.4
  • Flask version: git@25884c4
@davidism davidism added this to the 2.0.1 milestone May 21, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants