Use either the typing.TextIO or typing.BinaryIO types, for files opened in text mode or binary mode respectively.
From the docs:
class
typing.IOWrapper namespace for I/O stream types.
This defines the generic type
IO[AnyStr]and aliasesTextIOandBinaryIOfor respectivelyIO[str]andIO[bytes]. These representing the types of I/O streams such as returned byopen().
The short answer:
from typing import TextIO not just from typing import *.IO to mean a file without specifying what kindTextIO or BinaryIO if you know the typeAs an example:
from typing import BinaryIO
def binf(inf: BinaryIO):
pass
with open('x') as f:
binf(f)
gives an inspection error (in PyCharm) of Expected type 'BinaryIO', got 'TextIO' instead
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With