1

Previously typing.IO, typing.TextIO, and typing.BinaryIO were available to annotate file-like objects, e.g. those returned by open(). However, after Python 3.8 they seem to be deprecated, and will be removed in Python 3.12. Unlike many of the other deprecations like typing.List[T], which is replaced by list[T], these IO types have no clear migration path.

How should file-like object types be annotated in modern Python, going forward?

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
Migwell
  • 18,631
  • 21
  • 91
  • 160

1 Answers1

3

Python 3.9 docs has an unclear notice:

"These types are also in the typing.io namespace, which was never supported by type checkers and will be removed."

See more in the discussion "[docs] Confusing deprecation notice for typing.IO"

In the new version of the docs it's fixed:

"The typing.io namespace is deprecated and will be removed. These types should be directly imported from typing instead."

bad_coder
  • 11,289
  • 20
  • 44
  • 72
bartolo-otrit
  • 2,396
  • 3
  • 32
  • 50
  • 1
    Oh I see, so these types themselves aren't actually deprecated. We're just supposed to replace `typing.io.IO` with `typing.IO` etc going forward. – Migwell Sep 14 '21 at 07:13