From 7c20adb715957a83a25a1030fe73f6f5005bcfdf Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 2 Nov 2016 17:18:44 -0700 Subject: [PATCH] Fix memoryview stub (#651) Fixes python/mypy#2390 --- stdlib/3/builtins.pyi | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/stdlib/3/builtins.pyi b/stdlib/3/builtins.pyi index 3a3a13112..6ca58e106 100644 --- a/stdlib/3/builtins.pyi +++ b/stdlib/3/builtins.pyi @@ -1,7 +1,7 @@ # Stubs for builtins (Python 3) from typing import ( - TypeVar, Iterator, Iterable, overload, + TypeVar, Iterator, Iterable, overload, Container, Sequence, MutableSequence, Mapping, MutableMapping, Tuple, List, Any, Dict, Callable, Generic, Set, AbstractSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsBytes, SupportsAbs, SupportsRound, IO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional @@ -422,12 +422,41 @@ class bytearray(MutableSequence[int], ByteString): def __gt__(self, x: bytes) -> bool: ... def __ge__(self, x: bytes) -> bool: ... -class memoryview(): - # TODO arg can be any obj supporting the buffer protocol - def __init__(self, b: bytearray) -> None: ... + +class memoryview(Sized, Container[bytes]): + format = ... # type: str + itemsize = ... # type: int + shape = ... # type: Optional[Tuple[int, ...]] + strides = ... # type: Optional[Tuple[int, ...]] + suboffsets = ... # type: Optional[Tuple[int, ...]] + readonly = ... # type: bool + ndim = ... # type: int + + def __init__(self, obj: Union[str, bytes, bytearray, memoryview]) -> None: ... + + @overload + def __getitem__(self, i: int) -> int: ... + @overload + def __getitem__(self, s: slice) -> memoryview: ... + + def __contains__(self, x: object) -> bool: ... + def __iter__(self) -> Iterator[bytes]: ... + def __len__(self) -> int: ... + + @overload + def __setitem__(self, i: int, o: bytes) -> None: ... + @overload + def __setitem__(self, s: slice, o: Sequence[bytes]) -> None: ... + @overload + def __setitem__(self, s: slice, o: memoryview) -> None: ... + + def tobytes(self) -> bytes: ... + def tolist(self) -> List[int]: ... + if sys.version_info >= (3, 5): def hex(self) -> str: ... + class bool(int, SupportsInt, SupportsFloat): def __init__(self, o: object = ...) -> None: ... @@ -694,7 +723,6 @@ def map(func: Callable[[_T1, _T2], _S], iter1: Iterable[_T1], def max(arg1: _T, arg2: _T, *args: _T, key: Callable[[_T], Any] = ...) -> _T: ... @overload def max(iterable: Iterable[_T], key: Callable[[_T], Any] = ..., default:_T = ...) -> _T: ... -# TODO memoryview @overload def min(arg1: _T, arg2: _T, *args: _T, key: Callable[[_T], Any] = ...) -> _T: ... @overload