Use __new__ instead of __init__ for some builtin classes (#4555)

Closes #4514 #2630 #2686
This commit is contained in:
Sebastian Rittau
2020-09-30 09:00:06 +02:00
committed by GitHub
parent e3352c654d
commit 27dfbf68aa
3 changed files with 46 additions and 24 deletions

View File

@@ -182,9 +182,9 @@ class super(object):
class int:
@overload
def __init__(self, x: Union[Text, bytes, SupportsInt, _SupportsIndex] = ...) -> None: ...
def __new__(cls: Type[_T], x: Union[Text, bytes, SupportsInt, _SupportsIndex] = ...) -> _T: ...
@overload
def __init__(self, x: Union[Text, bytes, bytearray], base: int) -> None: ...
def __new__(cls: Type[_T], x: Union[Text, bytes, bytearray], base: int) -> _T: ...
if sys.version_info >= (3, 8):
def as_integer_ratio(self) -> Tuple[int, Literal[1]]: ...
@property
@@ -263,7 +263,7 @@ class int:
def __index__(self) -> int: ...
class float:
def __init__(self, x: Union[SupportsFloat, _SupportsIndex, Text, bytes, bytearray] = ...) -> None: ...
def __new__(cls: Type[_T], x: Union[SupportsFloat, _SupportsIndex, Text, bytes, bytearray] = ...) -> _T: ...
def as_integer_ratio(self) -> Tuple[int, int]: ...
def hex(self) -> str: ...
def is_integer(self) -> bool: ...
@@ -326,9 +326,9 @@ class float:
class complex:
@overload
def __init__(self, real: float = ..., imag: float = ...) -> None: ...
def __new__(cls: Type[_T], real: float = ..., imag: float = ...) -> _T: ...
@overload
def __init__(self, real: Union[str, SupportsComplex, _SupportsIndex]) -> None: ...
def __new__(cls: Type[_T], real: Union[str, SupportsComplex, _SupportsIndex]) -> _T: ...
@property
def real(self) -> float: ...
@property
@@ -447,9 +447,9 @@ class _FormatMapMapping(Protocol):
class str(Sequence[str], _str_base):
if sys.version_info >= (3,):
@overload
def __init__(self, o: object = ...) -> None: ...
def __new__(cls: Type[_T], o: object = ...) -> _T: ...
@overload
def __init__(self, o: bytes, encoding: str = ..., errors: str = ...) -> None: ...
def __new__(cls: Type[_T], o: bytes, encoding: str = ..., errors: str = ...) -> _T: ...
else:
def __init__(self, o: object = ...) -> None: ...
def capitalize(self) -> str: ...
@@ -597,15 +597,15 @@ class str(Sequence[str], _str_base):
if sys.version_info >= (3,):
class bytes(ByteString):
@overload
def __init__(self, ints: Iterable[int]) -> None: ...
def __new__(cls: Type[_T], ints: Iterable[int]) -> _T: ...
@overload
def __init__(self, string: str, encoding: str, errors: str = ...) -> None: ...
def __new__(cls: Type[_T], string: str, encoding: str, errors: str = ...) -> _T: ...
@overload
def __init__(self, length: int) -> None: ...
def __new__(cls: Type[_T], length: int) -> _T: ...
@overload
def __init__(self) -> None: ...
def __new__(cls: Type[_T]) -> _T: ...
@overload
def __init__(self, o: SupportsBytes) -> None: ...
def __new__(cls: Type[_T], o: SupportsBytes) -> _T: ...
def capitalize(self) -> bytes: ...
def center(self, __width: int, __fillchar: bytes = ...) -> bytes: ...
def count(self, sub: Union[bytes, int], start: Optional[int] = ..., end: Optional[int] = ...) -> int: ...