Skip to content

Commit fed4e03

Browse files
sproshevJelleZijlstra
authored andcommitted
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`
1 parent ed6dc17 commit fed4e03

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

stdlib/2/__builtin__.pyi

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ class type(object):
8888

8989
class int(SupportsInt, SupportsFloat, SupportsAbs[int]):
9090
@overload
91-
def __init__(self) -> None: ...
91+
def __init__(self, x: SupportsInt = ...) -> None: ...
92+
@overload
93+
def __init__(self, x: Union[str, unicode, bytearray], base: int = ...) -> None: ...
94+
9295
@overload
93-
def __init__(self, x: SupportsInt) -> None: ...
96+
def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ...
9497
@overload
95-
def __init__(self, x: Union[str, unicode, bytearray], base: int = 10) -> None: ...
98+
def __new__(cls: Type[_T1], x: Union[str, unicode, bytearray], base: int = ...) -> _T1: ...
99+
96100
def bit_length(self) -> int: ...
97101

98102
def __add__(self, x: int) -> int: ...
@@ -309,7 +313,8 @@ class unicode(basestring, Sequence[unicode]):
309313
def __hash__(self) -> int: ...
310314

311315
class str(basestring, Sequence[str]):
312-
def __init__(self, object: object='') -> None: ...
316+
def __init__(self, object: object = ...) -> None: ...
317+
def __new__(cls: Type[_T1], object: object = ...) -> _T1: ...
313318
def capitalize(self) -> str: ...
314319
def center(self, width: int, fillchar: str = ...) -> str: ...
315320
def count(self, x: unicode, __start: Optional[int] = ..., __end: Optional[int] = ...) -> int: ...

stdlib/3.4/enum.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ class Enum(metaclass=EnumMeta):
2424
name = ... # type: str
2525
value = ... # type: Any
2626

27-
class IntEnum(int, Enum):
27+
_T1 = TypeVar('_T1')
28+
29+
class IntEnum(int, Enum): # type: ignore
2830
value = ... # type: int
31+
def __new__(cls: Type[_T1], value: Any) -> _T1: ...
2932

3033
def unique(enumeration: _S) -> _S: ...
3134

stdlib/3/builtins.pyi

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,16 @@ class super:
103103
def __init__(self) -> None: ...
104104

105105
class int(SupportsInt, SupportsFloat, SupportsAbs[int]):
106-
def __init__(self, x: Union[SupportsInt, str, bytes] = ..., base: int = ...) -> None: ...
106+
@overload
107+
def __init__(self, x: SupportsInt = ...) -> None: ...
108+
@overload
109+
def __init__(self, x: Union[str, bytes], base: int = ...) -> None: ...
110+
111+
@overload
112+
def __new__(cls: Type[_T1], x: SupportsInt = ...) -> _T1: ...
113+
@overload
114+
def __new__(cls: Type[_T1], x: Union[str, bytes], base: int = ...) -> _T1: ...
115+
107116
def bit_length(self) -> int: ...
108117
def to_bytes(self, length: int, byteorder: str, *, signed: bool = ...) -> bytes: ...
109118
@classmethod
@@ -227,11 +236,15 @@ class complex(SupportsAbs[float]):
227236

228237
class str(Sequence[str]):
229238
@overload
230-
def __init__(self) -> None: ...
239+
def __init__(self, o: object = ...) -> None: ...
231240
@overload
232-
def __init__(self, o: object) -> None: ...
241+
def __init__(self, o: bytes, encoding: str = ..., errors: str = ...) -> None: ...
242+
243+
@overload
244+
def __new__(cls: Type[_T1], o: object = ...) -> _T1: ...
233245
@overload
234-
def __init__(self, o: bytes, encoding: str = ..., errors: str = 'strict') -> None: ...
246+
def __new__(cls: Type[_T1], o: bytes, encoding: str = ..., errors: str = ...) -> _T1: ...
247+
235248
def capitalize(self) -> str: ...
236249
def casefold(self) -> str: ...
237250
def center(self, width: int, fillchar: str = ' ') -> str: ...

third_party/2/enum.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Any, TypeVar
1+
from typing import List, Any, TypeVar, Type
22

33
class Enum:
44
def __new__(cls, value: Any) -> None: ...
@@ -12,8 +12,9 @@ class Enum:
1212
name = ... # type: str
1313
value = ... # type: Any
1414

15-
class IntEnum(int, Enum): ...
16-
1715
_T = TypeVar('_T')
1816

17+
class IntEnum(int, Enum): # type: ignore
18+
def __new__(cls: Type[_T], value: Any) -> _T: ...
19+
1920
def unique(enumeration: _T) -> _T: ...

0 commit comments

Comments
 (0)