计划在 Python 3.17 中移除¶
-
collections.abc.ByteString
is scheduled for removal in Python 3.17.Use
isinstance(obj, collections.abc.Buffer)
to test ifobj
implements the buffer protocol at runtime. For use in type annotations, either useBuffer
or a union that explicitly specifies the types your code supports (e.g.,bytes | bytearray | memoryview
).ByteString
was originally intended to be an abstract class that would serve as a supertype of bothbytes
andbytearray
. However, since the ABC never had any methods, knowing that an object was an instance ofByteString
never actually told you anything useful about the object. Other common buffer types such asmemoryview
were also never understood as subtypes ofByteString
(either at runtime or by static type checkers).See PEP 688 for more details. (Contributed by Shantanu Jain in gh-91896.)
-
在Python 3.14 之前,旧式的联合是通过私有类
typing._UnionGenericAlias
实现的。 实现已不再需要该类,但为向后兼容性保留了该类,并计划在 Python 3.17 中删除。 用户应使用已写入文档的内省助手函数,如typing.get_origin()
和typing.get_args()
,而不是依赖于私有的实现细节。typing.ByteString
, deprecated since Python 3.9, is scheduled for removal in Python 3.17.Use
isinstance(obj, collections.abc.Buffer)
to test ifobj
implements the buffer protocol at runtime. For use in type annotations, either useBuffer
or a union that explicitly specifies the types your code supports (e.g.,bytes | bytearray | memoryview
).ByteString
was originally intended to be an abstract class that would serve as a supertype of bothbytes
andbytearray
. However, since the ABC never had any methods, knowing that an object was an instance ofByteString
never actually told you anything useful about the object. Other common buffer types such asmemoryview
were also never understood as subtypes ofByteString
(either at runtime or by static type checkers).See PEP 688 for more details. (Contributed by Shantanu Jain in gh-91896.)