From ff0dedb8892a60a86a0a3c5c9da1dba3def39adf Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Thu, 25 May 2017 19:37:37 +0300 Subject: [PATCH 1/3] Add `__new__` to `str` and `int` stubs in both Pythons. --- stdlib/2/__builtin__.pyi | 13 +++++++++---- stdlib/3/builtins.pyi | 21 +++++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 541122b27bc3..e5f52e1ffd0e 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -88,11 +88,15 @@ class type(object): class int(SupportsInt, SupportsFloat, SupportsAbs[int]): @overload - def __init__(self) -> None: ... - @overload - def __init__(self, x: SupportsInt) -> None: ... + def __init__(self, x: SupportsInt = ...) -> None: ... @overload def __init__(self, x: Union[str, unicode, bytearray], base: int = 10) -> None: ... + + @overload + def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ... + @overload + def __new__(cls: Type[_T1], x: Union[str, unicode, bytearray], base: int = 10) -> _T1: ... + def bit_length(self) -> int: ... def __add__(self, x: int) -> int: ... @@ -309,7 +313,8 @@ class unicode(basestring, Sequence[unicode]): def __hash__(self) -> int: ... class str(basestring, Sequence[str]): - def __init__(self, object: object='') -> None: ... + def __init__(self, object: object = '') -> None: ... + def __new__(cls: Type[_T1], object: object = '') -> _T1: ... def capitalize(self) -> str: ... def center(self, width: int, fillchar: str = ...) -> str: ... def count(self, x: unicode, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... diff --git a/stdlib/3/builtins.pyi b/stdlib/3/builtins.pyi index f2a3b1b929c8..2b2067b1e827 100644 --- a/stdlib/3/builtins.pyi +++ b/stdlib/3/builtins.pyi @@ -103,7 +103,16 @@ class super: def __init__(self) -> None: ... class int(SupportsInt, SupportsFloat, SupportsAbs[int]): - def __init__(self, x: Union[SupportsInt, str, bytes] = ..., base: int = ...) -> None: ... + @overload + def __init__(self, x: SupportsInt = ...) -> None: ... + @overload + def __init__(self, x: Union[str, bytes], base: int = 10) -> None: ... + + @overload + def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ... + @overload + def __new__(cls: Type[_T1], x: Union[str, bytes], base: int = 10) -> _T1: ... + def bit_length(self) -> int: ... def to_bytes(self, length: int, byteorder: str, *, signed: bool = ...) -> bytes: ... @classmethod @@ -227,11 +236,15 @@ class complex(SupportsAbs[float]): class str(Sequence[str]): @overload - def __init__(self) -> None: ... - @overload - def __init__(self, o: object) -> None: ... + def __init__(self, o: object = '') -> None: ... @overload def __init__(self, o: bytes, encoding: str = ..., errors: str = 'strict') -> None: ... + + @overload + def __new__(cls: Type[_T1], o: object = '') -> _T1: ... + @overload + def __new__(cls: Type[_T1], o: bytes, encoding: str = ..., errors: str = 'strict') -> _T1: ... + def capitalize(self) -> str: ... def casefold(self) -> str: ... def center(self, width: int, fillchar: str = ' ') -> str: ... From f4b24de81d1f1ccba652228bcabe7e26dda7b92b Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Thu, 25 May 2017 20:47:34 +0300 Subject: [PATCH 2/3] Update default values to `...` in `__init__` and `__new__` in `int` and `str`. Add `__new__` to `enum.IntEnum` to override inherited `__new__`. --- stdlib/2/__builtin__.pyi | 8 ++++---- stdlib/3.4/enum.pyi | 3 +++ stdlib/3/builtins.pyi | 12 ++++++------ third_party/2/enum.pyi | 7 ++++--- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index e5f52e1ffd0e..e4193c22e906 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -90,12 +90,12 @@ class int(SupportsInt, SupportsFloat, SupportsAbs[int]): @overload def __init__(self, x: SupportsInt = ...) -> None: ... @overload - def __init__(self, x: Union[str, unicode, bytearray], base: int = 10) -> None: ... + def __init__(self, x: Union[str, unicode, bytearray], base: int = ...) -> None: ... @overload def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ... @overload - def __new__(cls: Type[_T1], x: Union[str, unicode, bytearray], base: int = 10) -> _T1: ... + def __new__(cls: Type[_T1], x: Union[str, unicode, bytearray], base: int = ...) -> _T1: ... def bit_length(self) -> int: ... @@ -313,8 +313,8 @@ class unicode(basestring, Sequence[unicode]): def __hash__(self) -> int: ... class str(basestring, Sequence[str]): - def __init__(self, object: object = '') -> None: ... - def __new__(cls: Type[_T1], object: object = '') -> _T1: ... + def __init__(self, object: object = ...) -> None: ... + def __new__(cls: Type[_T1], object: object = ...) -> _T1: ... def capitalize(self) -> str: ... def center(self, width: int, fillchar: str = ...) -> str: ... def count(self, x: unicode, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ... diff --git a/stdlib/3.4/enum.pyi b/stdlib/3.4/enum.pyi index a7e0fd686db3..ad42c7928b3f 100644 --- a/stdlib/3.4/enum.pyi +++ b/stdlib/3.4/enum.pyi @@ -24,8 +24,11 @@ class Enum(metaclass=EnumMeta): name = ... # type: str value = ... # type: Any +_T1 = TypeVar('_T1') + class IntEnum(int, Enum): value = ... # type: int + def __new__(cls: Type[_T1], value: Any) -> _T1: ... def unique(enumeration: _S) -> _S: ... diff --git a/stdlib/3/builtins.pyi b/stdlib/3/builtins.pyi index 2b2067b1e827..5d9ba56df67e 100644 --- a/stdlib/3/builtins.pyi +++ b/stdlib/3/builtins.pyi @@ -106,12 +106,12 @@ class int(SupportsInt, SupportsFloat, SupportsAbs[int]): @overload def __init__(self, x: SupportsInt = ...) -> None: ... @overload - def __init__(self, x: Union[str, bytes], base: int = 10) -> None: ... + def __init__(self, x: Union[str, bytes], base: int = ...) -> None: ... @overload def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ... @overload - def __new__(cls: Type[_T1], x: Union[str, bytes], base: int = 10) -> _T1: ... + def __new__(cls: Type[_T1], x: Union[str, bytes], base: int = ...) -> _T1: ... def bit_length(self) -> int: ... def to_bytes(self, length: int, byteorder: str, *, signed: bool = ...) -> bytes: ... @@ -236,14 +236,14 @@ class complex(SupportsAbs[float]): class str(Sequence[str]): @overload - def __init__(self, o: object = '') -> None: ... + def __init__(self, o: object = ...) -> None: ... @overload - def __init__(self, o: bytes, encoding: str = ..., errors: str = 'strict') -> None: ... + def __init__(self, o: bytes, encoding: str = ..., errors: str = ...) -> None: ... @overload - def __new__(cls: Type[_T1], o: object = '') -> _T1: ... + def __new__(cls: Type[_T1], o: object = ...) -> _T1: ... @overload - def __new__(cls: Type[_T1], o: bytes, encoding: str = ..., errors: str = 'strict') -> _T1: ... + def __new__(cls: Type[_T1], o: bytes, encoding: str = ..., errors: str = ...) -> _T1: ... def capitalize(self) -> str: ... def casefold(self) -> str: ... diff --git a/third_party/2/enum.pyi b/third_party/2/enum.pyi index 4032e818b80c..41906c7d5ceb 100644 --- a/third_party/2/enum.pyi +++ b/third_party/2/enum.pyi @@ -1,4 +1,4 @@ -from typing import List, Any, TypeVar +from typing import List, Any, TypeVar, Type class Enum: def __new__(cls, value: Any) -> None: ... @@ -12,8 +12,9 @@ class Enum: name = ... # type: str value = ... # type: Any -class IntEnum(int, Enum): ... - _T = TypeVar('_T') +class IntEnum(int, Enum): + def __new__(cls: Type[_T], value: Any) -> _T: ... + def unique(enumeration: _T) -> _T: ... From 08b2ac438e3f03c6c543a78f6adb1a468184e0d0 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Fri, 26 May 2017 16:12:13 +0300 Subject: [PATCH 3/3] Add `type: ignore` comment to `IntEnum` --- stdlib/3.4/enum.pyi | 2 +- third_party/2/enum.pyi | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/3.4/enum.pyi b/stdlib/3.4/enum.pyi index ad42c7928b3f..88a675602449 100644 --- a/stdlib/3.4/enum.pyi +++ b/stdlib/3.4/enum.pyi @@ -26,7 +26,7 @@ class Enum(metaclass=EnumMeta): _T1 = TypeVar('_T1') -class IntEnum(int, Enum): +class IntEnum(int, Enum): # type: ignore value = ... # type: int def __new__(cls: Type[_T1], value: Any) -> _T1: ... diff --git a/third_party/2/enum.pyi b/third_party/2/enum.pyi index 41906c7d5ceb..47f89900f8a9 100644 --- a/third_party/2/enum.pyi +++ b/third_party/2/enum.pyi @@ -14,7 +14,7 @@ class Enum: _T = TypeVar('_T') -class IntEnum(int, Enum): +class IntEnum(int, Enum): # type: ignore def __new__(cls: Type[_T], value: Any) -> _T: ... def unique(enumeration: _T) -> _T: ...