AttributeError: '_io.StringIO' object has no attribute 'buffer'?

38 views Asked by At

I have this function that simply writes a string into a stream buffer:

import sys
from typing import IO

def write(s: str, stream: IO[bytes] = sys.stdout.buffer):
    stream.write(s.encode())
    stream.flush()

I'm using both flake8 and mypy which don't complaint while pdoc throws an attribute error:

[...]

    def write(s: str, stream: IO[bytes] = sys.stdout.buffer):
AttributeError: '_io.StringIO' object has no attribute 'buffer'

Is there a way to fix this?

1

There are 1 answers

0
browser-bug On

Not really a solution but since stdout is a TextIO I've rewritten the function as:

def write(s: str, stream: TextIO = sys.stdout):
    stream.write(s)
    stream.flush()