Suggestion: SliceableBuffer type alias (#9115)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Avasam
2022-11-24 23:14:09 -05:00
committed by GitHub
parent 5f5449bcdf
commit be1da49957

View File

@@ -236,6 +236,29 @@ else:
ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable
_BufferWithLen: TypeAlias = ReadableBuffer # not stable # noqa: Y047
# Anything that implements the read-write buffer interface, and can be sliced/indexed.
SliceableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] | mmap.mmap
IndexableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] | mmap.mmap
# https://github.com/python/typeshed/pull/9115#issuecomment-1304905864
# Post PEP 688, they should be rewritten as such:
# from collections.abc import Sequence
# from typing import Sized, overload
# class SliceableBuffer(Protocol):
# def __buffer__(self, __flags: int) -> memoryview: ...
# def __getitem__(self, __slice: slice) -> Sequence[int]: ...
# class IndexableBuffer(Protocol):
# def __buffer__(self, __flags: int) -> memoryview: ...
# def __getitem__(self, __i: int) -> int: ...
# class SupportsGetItemBuffer(SliceableBuffer, IndexableBuffer, Protocol):
# def __buffer__(self, __flags: int) -> memoryview: ...
# def __contains__(self, __x: Any) -> bool: ...
# @overload
# def __getitem__(self, __slice: slice) -> Sequence[int]: ...
# @overload
# def __getitem__(self, __i: int) -> int: ...
# class SizedBuffer(Sized, Protocol): # instead of _BufferWithLen
# def __buffer__(self, __flags: int) -> memoryview: ...
ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
OptExcInfo: TypeAlias = Union[ExcInfo, tuple[None, None, None]]