Python Glossary Русский

Python 3.14+iofunction

io.Writer

protocols

The io.Writer[T] protocol: an abstract class with a write() method, used for annotations and structural isinstance/issubclass checks. Python 3.14+.

Syntax

io.Writer[T]

The check is structural: isinstance only looks for a write() method, so a byte-writing stream passes exactly like a text one — T matters to type checkers, not at runtime, and the method's signature is never verified. The protocol landed in 3.14, so code that must run on 3.13 or earlier should annotate with typing.IO/TextIO or declare its own Protocol instead.

Examples

import io
w = io.StringIO()
print(isinstance(w, io.Writer))        # → True
print(w.write('hi'))                   # → 2
print(w.getvalue())                    # → hi
print(isinstance('text', io.Writer))   # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy