typing.IO
Prev: typing.get_type_hints | Up: typing | Next: typing.NewType
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.IO
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import IO
Signature & Overview
typing.IO()
Generic base class for TextIO and BinaryIO.
Methods & Attributes
IO.close(self) -> None—IO.closemethod/property.IO.closed(...)—IO.closedmethod/property.IO.fileno(self) -> int—IO.filenomethod/property.IO.flush(self) -> None—IO.flushmethod/property.IO.isatty(self) -> bool—IO.isattymethod/property.IO.mode(...)—IO.modemethod/property.IO.name(...)—IO.namemethod/property.IO.read(self, n: int = -1) -> ~AnyStr—IO.readmethod/property.IO.readable(self) -> bool—IO.readablemethod/property.IO.readline(self, limit: int = -1) -> ~AnyStr—IO.readlinemethod/property.IO.readlines(self, hint: int = -1) -> list[~AnyStr]—IO.readlinesmethod/property.IO.seek(self, offset: int, whence: int = 0) -> int—IO.seekmethod/property.IO.seekable(self) -> bool—IO.seekablemethod/property.IO.tell(self) -> int—IO.tellmethod/property.IO.truncate(self, size: int | None = None) -> int—IO.truncatemethod/property.IO.writable(self) -> bool—IO.writablemethod/property.IO.write(self, s: ~AnyStr) -> int—IO.writemethod/property.IO.writelines(self, lines: list[~AnyStr]) -> None—IO.writelinesmethod/property.
Usage
import io
from typing import IO
def read_all(stream: IO[str]) -> str:
return stream.read()
assert read_all(io.StringIO("ok")) == "ok"
Gotchas
- Generic base of
TextIO(IO[str]) andBinaryIO(IO[bytes]): UseIO[Any]only when a function genuinely handles both text and binary streams.
REPL Inspect
from typing import IO
help(IO)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/get_type_hints Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/NewType Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_type shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing