Skip to content

BaseHTTPResponse.readinto should accept any bytes-like object not just bytearray #3764

Description

@hXtreme

Subject

BaseHTTPResponse.readinto is implemented to provide compatibility with the io module

# Compatibility methods for `io` module
def readinto(self, b: bytearray) -> int:
temp = self.read(len(b))
if len(temp) == 0:
return 0
else:
b[: len(temp)] = temp
return len(temp)

However the implementation's typehinting is not compatible with what python docs say are valid inputs for the method.

https://docs.python.org/3/library/io.html#io.RawIOBase.readinto

Read bytes into a pre-allocated, writable bytes-like object b

Where bytes-like objects can be any of:

object that supports the Buffer Protocol and can export a C-contiguous buffer. This includes all bytes, bytearray, and array.array objects, as well as many common memoryview objects. ...

And detail most relevant here would be the read-write variant of bytes-like object https://docs.python.org/3/glossary.html#term-bytes-like-object

Some operations need the binary data to be mutable. The documentation often refers to these as “read-write bytes-like objects”. Example mutable buffer objects include bytearray and a memoryview of a bytearray.

Environment

(not a runtime issue)

Steps to Reproduce

import io
import requests
from urllib3.response import HTTPResponse

with requests.get(url, stream=True) as response:
        data_stream = response.raw
        assert isinstance(data_stream, HTTPResponse)
        _ = io.BufferedReader(data_stream)

Run mypy on this snippet using typeshed for annotations for the io module

Expected Behavior

mypy should pass this piece of code

Actual Behavior

error: Argument 1 to "BufferedReader" has incompatible type "HTTPResponse"; expected "_BufferedReaderStream"  [arg-type]
note: Following member(s) of "HTTPResponse" have conflicts:
note:     Expected:
note:         def readinto(self, memoryview[int], /) -> int | None
note:     Got:
note:         def readinto(self, b: bytearray) -> int

I am also filing a issue with typeshed to improve the typing they provide for_BufferedReaderStream
python/typeshed#15311

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions