From fed4e03e5370058a1929db8c48ab5e9e93bbd350 Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Tue, 13 Jun 2017 06:53:32 +0300 Subject: [PATCH] Add `__new__` to `str` and `int` stubs in both Pythons. (#1352) * Update default values to `...` in `__init__` and `__new__` in `int` and `str`. * Add `__new__` to `enum.IntEnum` to override inherited `__new__`. * Add `type: ignore` comment to `IntEnum` --- stdlib/2/__builtin__.pyi | 13 +++++++++---- stdlib/3.4/enum.pyi | 5 ++++- stdlib/3/builtins.pyi | 21 +++++++++++++++++---- third_party/2/enum.pyi | 7 ++++--- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 33b25f56d..b7c39c79f 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: ... + def __init__(self, x: SupportsInt = ...) -> None: ... @overload - def __init__(self, x: SupportsInt) -> None: ... + def __init__(self, x: Union[str, unicode, bytearray], base: int = ...) -> None: ... + @overload - def __init__(self, x: Union[str, unicode, bytearray], base: int = 10) -> None: ... + def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ... + @overload + def __new__(cls: Type[_T1], x: Union[str, unicode, bytearray], base: int = ...) -> _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.4/enum.pyi b/stdlib/3.4/enum.pyi index dbb9df3b7..c2115f177 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 -class IntEnum(int, Enum): +_T1 = TypeVar('_T1') + +class IntEnum(int, Enum): # type: ignore 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 8ccd0363c..27d9c5eed 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 = ...) -> None: ... + + @overload + def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ... + @overload + 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: ... @classmethod @@ -227,11 +236,15 @@ class complex(SupportsAbs[float]): class str(Sequence[str]): @overload - def __init__(self) -> None: ... + def __init__(self, o: object = ...) -> None: ... @overload - def __init__(self, o: object) -> None: ... + def __init__(self, o: bytes, encoding: str = ..., errors: str = ...) -> None: ... + @overload - def __init__(self, o: bytes, encoding: str = ..., errors: str = 'strict') -> None: ... + def __new__(cls: Type[_T1], o: object = ...) -> _T1: ... + @overload + def __new__(cls: Type[_T1], o: bytes, encoding: str = ..., errors: str = ...) -> _T1: ... + def capitalize(self) -> str: ... def casefold(self) -> str: ... def center(self, width: int, fillchar: str = ' ') -> str: ... diff --git a/third_party/2/enum.pyi b/third_party/2/enum.pyi index 4032e818b..47f89900f 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): # type: ignore + def __new__(cls: Type[_T], value: Any) -> _T: ... + def unique(enumeration: _T) -> _T: ...