如何在 Python 3.8+ 中对类文件对象进行类型注释
How to type-annotate a file-like object in Python 3.8+
以前 typing.IO
、typing.TextIO
和 typing.BinaryIO
可用于注释类似文件的对象,例如open()
返回的那些。但是,在 Python 3.8 之后,它们似乎已被弃用,并将在 Python 3.12 中删除。与许多其他弃用(例如 typing.List[T]
被 list[T]
取代不同,这些 IO 类型没有明确的迁移路径。
未来应该如何在现代 Python 中注释类似文件的对象类型?
Python 3.9 docs 有一个不清楚的通知:
"These types are also in the typing.io
namespace, which was never supported by type checkers and will be removed."
在讨论中查看更多内容"[docs] Confusing deprecation notice for typing.IO"
在文档的 new version 中已修复:
"The typing.io
namespace is deprecated and will be removed. These types should be directly imported from typing instead."
以前 typing.IO
、typing.TextIO
和 typing.BinaryIO
可用于注释类似文件的对象,例如open()
返回的那些。但是,在 Python 3.8 之后,它们似乎已被弃用,并将在 Python 3.12 中删除。与许多其他弃用(例如 typing.List[T]
被 list[T]
取代不同,这些 IO 类型没有明确的迁移路径。
未来应该如何在现代 Python 中注释类似文件的对象类型?
Python 3.9 docs 有一个不清楚的通知:
"These types are also in the
typing.io
namespace, which was never supported by type checkers and will be removed."
在讨论中查看更多内容"[docs] Confusing deprecation notice for typing.IO"
在文档的 new version 中已修复:
"The
typing.io
namespace is deprecated and will be removed. These types should be directly imported from typing instead."