1

I have the following class:

class Unbuffered:
    def __init__(self, stream, logfile):
        self.stream = stream
        self.logfile = logfile

    def write(self, data):
        if self.stream is not None:
            self.stream.write(data)
            self.stream.flush()
        # Write the data of stdout here to a text file as well
        if not self.logfile.closed:
            self.logfile.write(data)
            self.logfile.write("\n")

    def flush(self):
        pass

and the following sentence:

sys.stdout = Unbuffered(sys.stdout, logfile)

The problem is that linter mypy marks this error in the latest:

error: Incompatible types in assignment (expression has type "Unbuffered", variable has type "TextIO")

How can I avoid it?

HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93
  • Does this answer your question? [Disable output buffering](https://stackoverflow.com/questions/107705/disable-output-buffering) – Jan Wilamowski Apr 20 '22 at 02:32
  • 1
    You see this error, because `Unbuffered` class is not a subclass of `TextIO`. You can inherit from some IO class to avoid it. For example, inheriting `io.TextIOWrapper` works well. – STerliakov Apr 21 '22 at 08:21

0 Answers0