From e5dcf06c9e68969fc28fb0a869e281f15cf4a1dd Mon Sep 17 00:00:00 2001 From: Steve Dignam Date: Fri, 15 May 2020 05:55:40 -0400 Subject: [PATCH] Update array with more precise type for typecodes (#3994) Fixes #3946 --- stdlib/2and3/array.pyi | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/stdlib/2and3/array.pyi b/stdlib/2and3/array.pyi index 4f0b8a2cb..5b9c1256e 100644 --- a/stdlib/2and3/array.pyi +++ b/stdlib/2and3/array.pyi @@ -5,6 +5,12 @@ import sys from typing import (Any, BinaryIO, Generic, Iterable, Iterator, List, MutableSequence, overload, Text, Tuple, TypeVar, Union) +from typing_extensions import Literal + +_IntTypeCode = Literal['b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q'] +_FloatTypeCode = Literal['f', 'd'] +_UnicodeTypeCode = Literal['u'] +_TypeCode = Union[_IntTypeCode, _FloatTypeCode, _UnicodeTypeCode] _T = TypeVar('_T', int, float, Text) @@ -12,8 +18,18 @@ if sys.version_info >= (3,): typecodes: str class array(MutableSequence[_T], Generic[_T]): - typecode: str + typecode: _TypeCode itemsize: int + @overload + def __init__(self: array[int], typecode: _IntTypeCode, + __initializer: Union[bytes, Iterable[_T]] = ...) -> None: ... + @overload + def __init__(self: array[float], typecode: _FloatTypeCode, + __initializer: Union[bytes, Iterable[_T]] = ...) -> None: ... + @overload + def __init__(self: array[Text], typecode: _UnicodeTypeCode, + __initializer: Union[bytes, Iterable[_T]] = ...) -> None: ... + @overload def __init__(self, typecode: str, __initializer: Union[bytes, Iterable[_T]] = ...) -> None: ... def append(self, __v: _T) -> None: ...