|
2 | 2 | # |
3 | 3 | # See the README.md file in this directory for more information. |
4 | 4 |
|
5 | | -import array |
6 | | -import ctypes |
7 | | -import mmap |
8 | | -import pickle |
9 | 5 | import sys |
10 | | -from collections.abc import Awaitable, Callable, Iterable, Set as AbstractSet |
| 6 | +from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized |
11 | 7 | from dataclasses import Field |
12 | 8 | from os import PathLike |
13 | 9 | from types import FrameType, TracebackType |
14 | | -from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar |
15 | | -from typing_extensions import Final, Literal, LiteralString, TypeAlias, final |
| 10 | +from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar, overload |
| 11 | +from typing_extensions import Buffer, Final, Literal, LiteralString, TypeAlias, final |
16 | 12 |
|
17 | 13 | _KT = TypeVar("_KT") |
18 | 14 | _KT_co = TypeVar("_KT_co", covariant=True) |
@@ -227,42 +223,33 @@ class SupportsNoArgReadline(Protocol[_T_co]): |
227 | 223 | class SupportsWrite(Protocol[_T_contra]): |
228 | 224 | def write(self, __s: _T_contra) -> object: ... |
229 | 225 |
|
230 | | -ReadOnlyBuffer: TypeAlias = bytes # stable |
| 226 | +# Unfortunately PEP 688 does not allow us to distinguish read-only |
| 227 | +# from writable buffers. We use these aliases for readability for now. |
| 228 | +# Perhaps a future extension of the buffer protocol will allow us to |
| 229 | +# distinguish these cases in the type system. |
| 230 | +ReadOnlyBuffer: TypeAlias = Buffer # stable |
231 | 231 | # Anything that implements the read-write buffer interface. |
232 | | -# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol |
233 | | -# for it (until PEP 688 is implemented). Instead we have to list the most common stdlib buffer classes in a Union. |
234 | | -if sys.version_info >= (3, 8): |
235 | | - WriteableBuffer: TypeAlias = ( |
236 | | - bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData | pickle.PickleBuffer |
237 | | - ) # stable |
238 | | -else: |
239 | | - WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable |
240 | | -# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes). |
241 | | -ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable |
242 | | -_BufferWithLen: TypeAlias = ReadableBuffer # not stable # noqa: Y047 |
243 | | - |
244 | | -# Anything that implements the read-write buffer interface, and can be sliced/indexed. |
245 | | -SliceableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] | mmap.mmap |
246 | | -IndexableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] | mmap.mmap |
247 | | -# https://github.com/python/typeshed/pull/9115#issuecomment-1304905864 |
248 | | -# Post PEP 688, they should be rewritten as such: |
249 | | -# from collections.abc import Sequence |
250 | | -# from typing import Sized, overload |
251 | | -# class SliceableBuffer(Protocol): |
252 | | -# def __buffer__(self, __flags: int) -> memoryview: ... |
253 | | -# def __getitem__(self, __slice: slice) -> Sequence[int]: ... |
254 | | -# class IndexableBuffer(Protocol): |
255 | | -# def __buffer__(self, __flags: int) -> memoryview: ... |
256 | | -# def __getitem__(self, __i: int) -> int: ... |
257 | | -# class SupportsGetItemBuffer(SliceableBuffer, IndexableBuffer, Protocol): |
258 | | -# def __buffer__(self, __flags: int) -> memoryview: ... |
259 | | -# def __contains__(self, __x: Any) -> bool: ... |
260 | | -# @overload |
261 | | -# def __getitem__(self, __slice: slice) -> Sequence[int]: ... |
262 | | -# @overload |
263 | | -# def __getitem__(self, __i: int) -> int: ... |
264 | | -# class SizedBuffer(Sized, Protocol): # instead of _BufferWithLen |
265 | | -# def __buffer__(self, __flags: int) -> memoryview: ... |
| 232 | +WriteableBuffer: TypeAlias = Buffer |
| 233 | +# Same as WriteableBuffer, but also includes read-only buffer types (like bytes). |
| 234 | +ReadableBuffer: TypeAlias = Buffer # stable |
| 235 | + |
| 236 | +class SliceableBuffer(Buffer, Protocol): |
| 237 | + def __getitem__(self, __slice: slice) -> Sequence[int]: ... |
| 238 | + |
| 239 | +class IndexableBuffer(Buffer, Protocol): |
| 240 | + def __getitem__(self, __i: int) -> int: ... |
| 241 | + |
| 242 | +class SupportsGetItemBuffer(SliceableBuffer, IndexableBuffer, Protocol): |
| 243 | + def __contains__(self, __x: Any) -> bool: ... |
| 244 | + @overload |
| 245 | + def __getitem__(self, __slice: slice) -> Sequence[int]: ... |
| 246 | + @overload |
| 247 | + def __getitem__(self, __i: int) -> int: ... |
| 248 | + |
| 249 | +class SizedBuffer(Sized, Buffer, Protocol): ... |
| 250 | + |
| 251 | +# for compatibility with third-party stubs that may use this |
| 252 | +_BufferWithLen: TypeAlias = SizedBuffer # not stable # noqa: Y047 |
266 | 253 |
|
267 | 254 | ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType] |
268 | 255 | OptExcInfo: TypeAlias = ExcInfo | tuple[None, None, None] |
|
0 commit comments