From 4c0dccac0ffd0bf3bcc9597f5dfbc45d5ffee609 Mon Sep 17 00:00:00 2001 From: Vincent Pelletier Date: Tue, 28 Sep 2021 16:15:26 +0900 Subject: [PATCH] Fold remaining custom stdlib *Buffer types into _typeshed. (#6082) Add ctypes base type to WriteableBuffer. Add a ReadOnlyBuffer type from fcntl. Base ReadableBuffer on WriteableBuffer and ReadOnlyBuffer. Use these types in fcntl and ctypes stubs. --- stdlib/_typeshed/__init__.pyi | 10 ++++++++-- stdlib/ctypes/__init__.pyi | 13 +++---------- stdlib/fcntl.pyi | 15 +++++---------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 3f8978a64..9e60fd07e 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -3,6 +3,7 @@ # See the README.md file in this directory for more information. import array +import ctypes import mmap import sys from os import PathLike @@ -167,8 +168,13 @@ class SupportsNoArgReadline(Protocol[_T_co]): class SupportsWrite(Protocol[_T_contra]): def write(self, __s: _T_contra) -> Any: ... -ReadableBuffer = Union[bytes, bytearray, memoryview, array.array[Any], mmap.mmap] # stable -WriteableBuffer = Union[bytearray, memoryview, array.array[Any], mmap.mmap] # stable +ReadOnlyBuffer = bytes # stable +# Anything that implements the read-write buffer interface. +# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol +# for it. Instead we have to list the most common stdlib buffer classes in a Union. +WriteableBuffer = Union[bytearray, memoryview, array.array[Any], mmap.mmap, ctypes._CData] # stable +# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes). +ReadableBuffer = Union[ReadOnlyBuffer, WriteableBuffer] # stable # stable if sys.version_info >= (3, 10): diff --git a/stdlib/ctypes/__init__.pyi b/stdlib/ctypes/__init__.pyi index 03e9affd5..696705833 100644 --- a/stdlib/ctypes/__init__.pyi +++ b/stdlib/ctypes/__init__.pyi @@ -1,5 +1,5 @@ import sys -from array import array +from _typeshed import ReadableBuffer, WriteableBuffer from typing import ( Any, Callable, @@ -72,13 +72,6 @@ if sys.platform == "win32": pydll: LibraryLoader[PyDLL] pythonapi: PyDLL -# Anything that implements the read-write buffer interface. -# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol -# for it. Instead we have to list the most common stdlib buffer classes in a Union. -_WritableBuffer = _UnionT[bytearray, memoryview, array[Any], _CData] -# Same as _WritableBuffer, but also includes read-only buffer types (like bytes). -_ReadOnlyBuffer = _UnionT[_WritableBuffer, bytes] - class _CDataMeta(type): # By default mypy complains about the following two methods, because strictly speaking cls # might not be a Type[_CT]. However this can never actually happen, because the only class that @@ -91,9 +84,9 @@ class _CData(metaclass=_CDataMeta): _b_needsfree_: bool _objects: Mapping[Any, int] | None @classmethod - def from_buffer(cls: Type[_CT], source: _WritableBuffer, offset: int = ...) -> _CT: ... + def from_buffer(cls: Type[_CT], source: WriteableBuffer, offset: int = ...) -> _CT: ... @classmethod - def from_buffer_copy(cls: Type[_CT], source: _ReadOnlyBuffer, offset: int = ...) -> _CT: ... + def from_buffer_copy(cls: Type[_CT], source: ReadableBuffer, offset: int = ...) -> _CT: ... @classmethod def from_address(cls: Type[_CT], address: int) -> _CT: ... @classmethod diff --git a/stdlib/fcntl.pyi b/stdlib/fcntl.pyi index ebaa31749..141f9ee93 100644 --- a/stdlib/fcntl.pyi +++ b/stdlib/fcntl.pyi @@ -1,7 +1,6 @@ import sys -from _typeshed import FileDescriptorLike -from array import array -from typing import Any, Union, overload +from _typeshed import FileDescriptorLike, ReadOnlyBuffer, WriteableBuffer +from typing import Any, overload from typing_extensions import Literal FASYNC: int @@ -85,17 +84,13 @@ LOCK_WRITE: int def fcntl(__fd: FileDescriptorLike, __cmd: int, __arg: int = ...) -> int: ... @overload def fcntl(__fd: FileDescriptorLike, __cmd: int, __arg: bytes) -> bytes: ... - -_ReadOnlyBuffer = bytes -_WritableBuffer = Union[bytearray, memoryview, array[Any]] - @overload def ioctl(__fd: FileDescriptorLike, __request: int, __arg: int = ..., __mutate_flag: bool = ...) -> int: ... @overload -def ioctl(__fd: FileDescriptorLike, __request: int, __arg: _WritableBuffer, __mutate_flag: Literal[True] = ...) -> int: ... +def ioctl(__fd: FileDescriptorLike, __request: int, __arg: WriteableBuffer, __mutate_flag: Literal[True] = ...) -> int: ... @overload -def ioctl(__fd: FileDescriptorLike, __request: int, __arg: _WritableBuffer, __mutate_flag: Literal[False]) -> bytes: ... +def ioctl(__fd: FileDescriptorLike, __request: int, __arg: WriteableBuffer, __mutate_flag: Literal[False]) -> bytes: ... @overload -def ioctl(__fd: FileDescriptorLike, __request: int, __arg: _ReadOnlyBuffer, __mutate_flag: bool = ...) -> bytes: ... +def ioctl(__fd: FileDescriptorLike, __request: int, __arg: ReadOnlyBuffer, __mutate_flag: bool = ...) -> bytes: ... def flock(__fd: FileDescriptorLike, __operation: int) -> None: ... def lockf(__fd: FileDescriptorLike, __cmd: int, __len: int = ..., __start: int = ..., __whence: int = ...) -> Any: ...