From 29473745a8fdad44bc8694c5098a48f9952615a0 Mon Sep 17 00:00:00 2001 From: hashstat Date: Mon, 9 Oct 2017 10:12:01 -0700 Subject: [PATCH] Implement array.array as a MutableSequence (#1649) It also improves the type checking of contained values. Some methods were removed because they are implemented by a base class (i.e., *__iter__()*, *__str__()*, and *__contains__()*). *__hash__()* was removed because arrays are unhashable types. --- stdlib/3/array.pyi | 50 +++++++++++++++++++++++--------------- tests/pytype_blacklist.txt | 5 ++++ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/stdlib/3/array.pyi b/stdlib/3/array.pyi index 77ed0524c..c740556b7 100644 --- a/stdlib/3/array.pyi +++ b/stdlib/3/array.pyi @@ -2,48 +2,58 @@ # Based on http://docs.python.org/3.2/library/array.html -from typing import Any, Iterable, Tuple, List, Iterator, BinaryIO, overload +from typing import (Any, BinaryIO, Generic, Iterable, Iterator, List, MutableSequence, + overload, Text, Tuple, TypeVar, Union) + +_T = TypeVar('_T', int, float, Text) typecodes = ... # type: str -class array: +class array(MutableSequence[_T], Generic[_T]): typecode = ... # type: str itemsize = ... # type: int def __init__(self, typecode: str, - initializer: Iterable[Any] = ...) -> None: ... - def append(self, x: Any) -> None: ... + __initializer: Union[bytes, Iterable[_T]] = ...) -> None: ... + def append(self, x: _T) -> None: ... def buffer_info(self) -> Tuple[int, int]: ... def byteswap(self) -> None: ... def count(self, x: Any) -> int: ... - def extend(self, iterable: Iterable[Any]) -> None: ... + def extend(self, iterable: Iterable[_T]) -> None: ... def frombytes(self, s: bytes) -> None: ... def fromfile(self, f: BinaryIO, n: int) -> None: ... - def fromlist(self, list: List[Any]) -> None: ... + def fromlist(self, list: List[_T]) -> None: ... def fromstring(self, s: bytes) -> None: ... def fromunicode(self, s: str) -> None: ... - def index(self, x: Any) -> int: ... - def insert(self, i: int, x: Any) -> None: ... - def pop(self, i: int = ...) -> Any: ... + def index(self, x: _T) -> int: ... # type: ignore # Overrides Sequence + def insert(self, i: int, x: _T) -> None: ... + def pop(self, i: int = ...) -> _T: ... def remove(self, x: Any) -> None: ... def reverse(self) -> None: ... def tobytes(self) -> bytes: ... def tofile(self, f: BinaryIO) -> None: ... - def tolist(self) -> List[Any]: ... + def tolist(self) -> List[_T]: ... def tostring(self) -> bytes: ... def tounicode(self) -> str: ... def __len__(self) -> int: ... - def __iter__(self) -> Iterator[Any]: ... - def __str__(self) -> str: ... - def __hash__(self) -> int: ... @overload - def __getitem__(self, i: int) -> Any: ... + def __getitem__(self, i: int) -> _T: ... @overload - def __getitem__(self, s: slice) -> 'array': ... + def __getitem__(self, s: slice) -> array[_T]: ... - def __setitem__(self, i: int, o: Any) -> None: ... - def __delitem__(self, i: int) -> None: ... - def __add__(self, x: 'array') -> 'array': ... - def __mul__(self, n: int) -> 'array': ... - def __contains__(self, o: object) -> bool: ... + @overload # type: ignore # Overrides MutableSequence + def __setitem__(self, i: int, o: _T) -> None: ... + @overload + def __setitem__(self, s: slice, o: array[_T]) -> None: ... + + def __delitem__(self, i: Union[int, slice]) -> None: ... + def __add__(self, x: array[_T]) -> array[_T]: ... + def __ge__(self, other: array[_T]) -> bool: ... + def __gt__(self, other: array[_T]) -> bool: ... + def __iadd__(self, x: array[_T]) -> array[_T]: ... # type: ignore # Overrides MutableSequence + def __imul__(self, n: int) -> array[_T]: ... + def __le__(self, other: array[_T]) -> bool: ... + def __lt__(self, other: array[_T]) -> bool: ... + def __mul__(self, n: int) -> array[_T]: ... + def __rmul__(self, n: int) -> array[_T]: ... diff --git a/tests/pytype_blacklist.txt b/tests/pytype_blacklist.txt index 332c8a144..b3faed16a 100644 --- a/tests/pytype_blacklist.txt +++ b/tests/pytype_blacklist.txt @@ -8,6 +8,11 @@ stdlib/2/typing.pyi stdlib/3/builtins.pyi stdlib/3/typing.pyi +# pytype doesn't yet support "# type: ignore" after decorators, sometimes +# used when overriding methods, especially those inherited from container +# abstract base classes: +stdlib/3/array.pyi + # Because of 'from . import path': stdlib/2/os/__init__.pyi stdlib/3/os/__init__.pyi