I have a function that looks a bit like this. I want the function to accept any subclass of io.IOBase - in other words, any file-like object.
def import_csv_
If you annotate a function argument with the base class (io.IOBase
in your case) then you can also pass instances of any subtype of the base class – inheritance applies to annotation types as well.
That said, you could use typing.IO as a generic type representing any I/O stream (and typing.TextIO
and typing.BinaryIO
for binary and text I/O streams respectively).