Skip to content

VSCode/Pylance and mypy emit error on valid consumption of the StreamingHttpResponse.streaming_content iterator #2679

Open
@biblicabeebli

Description

@biblicabeebli

Bug report

What's wrong

Getting an error on a reasonable line in my test suite involving a StreamingHttpResponse.streaming_content in vscode / pylance and mypy.

As long as the type is identified by the type checker it will emit the error, stick this anywhere:

from django.http import StreamingHttpResponse
g = (str(x).encode() for x in range(100))  # does not even matter if this is real....
response = StreamingHttpResponse(g, status_code=200)
content = b"".join(response.streaming_content)  # <- mypy/pylance error

The code iterates over it just fine, the join returns bytes.

mypy emits:

error: Argument 1 to "join" of "bytes" has incompatible type "Iterator[bytes] | AsyncIterator[bytes]"; expected "Iterable[Buffer]"  [arg-type]

in VSCode the Pylance popup states

Argument of type "Iterable[object] | AsyncIterable[object]" cannot be assigned to parameter "iterable_of_bytes" of type "Iterable[ReadableBuffer]" in function "join"
  Type "Iterable[object] | AsyncIterable[object]" is not assignable to type "Iterable[ReadableBuffer]"
    "AsyncIterable[object]" is incompatible with protocol "Iterable[ReadableBuffer]"
      "__iter__" is not presentPylance[reportArgumentType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType)
(variable) streaming_content: Iterable[object] | AsyncIterable[object]

What would be correct

No type warning/error in the type checker there.
(Maybe I'm missing something subtle about typing, but imo that code should pass type inspection.)

It seems likely that swapping bytes for buffer/Buffer fixes this, but I'm nowhere near fluent enough with this stuff to proceed further. The detail with async being incompatible seems like a different problem, but again, this just is not at my level.

System information

  • OS:
  • python version: 3.12.x
  • django version: 4.2.21
  • mypy version: 1.15.0
  • django-stubs version: 5.2.0
  • django-stubs-ext version: 5.2.0
    Testing on the most recent version of pylance included with vscode, not the beta.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions