Python 3.5+typingterm
typing.TextIO
I/O streams
The type for text file streams in annotations.
Syntax
def f(stream: typing.TextIO) -> None: ...
This is an abstract stand-in type for annotations, not a class you instantiate (hence the example touches TextIO.__name__ rather than an instance). Annotate with it when your function accepts any text stream — a file from open() in text mode, io.StringIO, or sys.stdout — rather than one concrete file type.
Examples
from typing import IO, TextIO
import io
print(TextIO.__name__) # → TextIO
from typing import IO, TextIO
import io
def read_all(f: TextIO) -> str: return f.read()
print(read_all(io.StringIO('hello'))) # → hello
from typing import IO, TextIO
import io
def first_word(f: TextIO) -> str: return f.read().split()[0]
print(first_word(io.StringIO('раз два три'))) # → раз
print(issubclass(TextIO, IO)) # → True
print(isinstance(io.StringIO(), TextIO)) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy