In python type-hinting, how can I make an argument accept any subclass of a base class?
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).