I am writing stuff to an in-memory binary stream in order to upload stuff to S3 without storing it on a local file (I have more memory than disk space). The following code is working, but mypy mvce.py fails with
mvce.py:6: error: Argument 1 to "TextIOWrapper" has incompatible type "GzipFile";
expected "IO[bytes]"
Found 1 error in 1 file (checked 1 source file)
mvce.py
from io import BytesIO, TextIOWrapper
import gzip
inmem = BytesIO()
with gzip.GzipFile(fileobj=inmem, mode="wb") as gzip_handler, TextIOWrapper(
gzip_handler, encoding="utf-8"
) as wrapper:
wrapper.write("some test string")
# Check if this actually worked
with open("foobar.gzip", "wb") as f1:
inmem.seek(0)
f1.write(inmem.read())
with gzip.open("foobar.gzip", "rb") as f2:
data = f2.read()
print(data)
Question
Why does mypy fail and how do I make it work? Are there hidden potential issues?