'Python substitute sys.stdout causes mypy error

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source