Is there any correct type hint to use for a file or file-like object in Python? For example, how would I type-hint the return value of this function?
def foo():
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