1

I am trying to create a BufferedReader from a BytesIO object but my IDE (PyCharm) is warning me that BufferedReader Expected type 'RawIOBase', got 'BytesIO' instead.

The logic seems to work but I'd like to solve this properly if there is a way to do so.

Another way to phrase this question would be: can BufferedReader wrap a BytesIO object or can it only do so with objects that extend the RawIOBase interface?

Minimum reproducible code below:

from io import BytesIO, BufferedReader

b_handle = BytesIO()
b_handle.write(b"Hello World")
b_handle.seek(0)

# Warning: Expected type 'RawIOBase', got 'BytesIO' instead

br = BufferedReader(b_handle)

data = br.read()
print(data)
Bemwa Malak
  • 1,182
  • 1
  • 5
  • 18
keith.g
  • 900
  • 9
  • 19
  • 2
    If you have a `BytesIO`, then it's already in-memory so the buffering wouldn't really buy you much - or I might be missing the use case? – slothrop Apr 20 '23 at 16:18
  • 1
    Existing answers on this suggest just using the `BytesIO` directly, e.g. https://stackoverflow.com/questions/47198948/convert-bytes-into-bufferedreader-in-python – slothrop Apr 20 '23 at 16:18
  • You're right, using the `BytesIO` object directly does work. – keith.g Apr 20 '23 at 16:34

0 Answers0