Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use __new__ instead of __init__ for some builtin classes #4555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions stdlib/2/__builtin__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,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
Expand Down Expand Up @@ -262,7 +262,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: ...
Expand Down Expand Up @@ -325,9 +325,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
Expand Down Expand Up @@ -446,9 +446,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: ...
Expand Down Expand Up @@ -596,15 +596,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: ...
Expand Down
24 changes: 12 additions & 12 deletions stdlib/2and3/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,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
Expand Down Expand Up @@ -262,7 +262,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: ...
Expand Down Expand Up @@ -325,9 +325,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
Expand Down Expand Up @@ -446,9 +446,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: ...
Expand Down Expand Up @@ -596,15 +596,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: ...
Expand Down
22 changes: 22 additions & 0 deletions tests/stubtest_whitelists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,28 @@ zipfile.ZipExtFile.read
zipfile.ZipExtFile.readline
zlib.compressobj

# These enums derive from (int, IntEnum) or (str, Enum). Strangely,
# at runtime, they inherit Enum.__new__, not int.__new__ or
# str.__new__ as the mro would dictate, which is why stubtest balks.
enum.IntEnum.__new__
enum.IntFlag.__new__
http.HTTPStatus.__new__
inspect._ParameterKind.__new__
pstats.SortKey.__new__
re.RegexFlag.__new__
signal.Handlers.__new__
signal.Sigmasks.__new__
signal.Signals.__new__
socket.AddressFamily.__new__
socket.AddressInfo.__new__
socket.MsgFlag.__new__
socket.SocketKind.__new__
ssl.Options.__new__
ssl.TLSVersion.__new__
ssl.VerifyFlags.__new__
ssl.VerifyMode.__new__
tkinter.EventType.__new__

# ==========
# Whitelist entries that cannot or should not be fixed
# ==========
Expand Down