Skip to content

Commit 8a7d617

Browse files
srittauJelleZijlstra
authored andcommitted
Python3.8 additions and changes (#3337)
* Add as_integer_ratio() to a few types * Add dirs_exist_ok to copytree() * int, float, complex accept __index__ args Also fix complex.__init__ argument names * Add __reversed__ to dict et al. * Python 3.8 date(time) arithmetic fixes * Add CodeType.replace()
1 parent d0beab9 commit 8a7d617

File tree

7 files changed

+97
-27
lines changed

7 files changed

+97
-27
lines changed

stdlib/2/__builtin__.pyi

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import sys
1717
if sys.version_info >= (3,):
1818
from typing import SupportsBytes, SupportsRound
1919

20+
if sys.version_info >= (3, 8):
21+
from typing import Literal
22+
else:
23+
from typing_extensions import Literal
24+
2025
_T = TypeVar('_T')
2126
_T_co = TypeVar('_T_co', covariant=True)
2227
_KT = TypeVar('_KT')
@@ -29,6 +34,9 @@ _T4 = TypeVar('_T4')
2934
_T5 = TypeVar('_T5')
3035
_TT = TypeVar('_TT', bound='type')
3136

37+
class _SupportsIndex(Protocol):
38+
def __index__(self) -> int: ...
39+
3240
class object:
3341
__doc__: Optional[str]
3442
__dict__: Dict[str, Any]
@@ -129,10 +137,12 @@ class super(object):
129137

130138
class int:
131139
@overload
132-
def __init__(self, x: Union[Text, bytes, SupportsInt] = ...) -> None: ...
140+
def __init__(self, x: Union[Text, bytes, SupportsInt, _SupportsIndex] = ...) -> None: ...
133141
@overload
134142
def __init__(self, x: Union[Text, bytes, bytearray], base: int) -> None: ...
135143

144+
if sys.version_info >= (3, 8):
145+
def as_integer_ratio(self) -> Tuple[int, Literal[1]]: ...
136146
@property
137147
def real(self) -> int: ...
138148
@property
@@ -209,7 +219,7 @@ class int:
209219
def __index__(self) -> int: ...
210220

211221
class float:
212-
def __init__(self, x: Union[SupportsFloat, Text, bytes, bytearray] = ...) -> None: ...
222+
def __init__(self, x: Union[SupportsFloat, _SupportsIndex, Text, bytes, bytearray] = ...) -> None: ...
213223
def as_integer_ratio(self) -> Tuple[int, int]: ...
214224
def hex(self) -> str: ...
215225
def is_integer(self) -> bool: ...
@@ -271,11 +281,9 @@ class float:
271281

272282
class complex:
273283
@overload
274-
def __init__(self, re: float = ..., im: float = ...) -> None: ...
275-
@overload
276-
def __init__(self, s: str) -> None: ...
284+
def __init__(self, real: float = ..., imag: float = ...) -> None: ...
277285
@overload
278-
def __init__(self, s: SupportsComplex) -> None: ...
286+
def __init__(self, real: Union[str, SupportsComplex, _SupportsIndex]) -> None: ...
279287

280288
@property
281289
def real(self) -> float: ...
@@ -987,6 +995,8 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
987995
def __setitem__(self, k: _KT, v: _VT) -> None: ...
988996
def __delitem__(self, v: _KT) -> None: ...
989997
def __iter__(self) -> Iterator[_KT]: ...
998+
if sys.version_info >= (3, 8):
999+
def __reversed__(self) -> Iterator[_KT]: ...
9901000
def __str__(self) -> str: ...
9911001
__hash__: None # type: ignore
9921002

@@ -1117,8 +1127,6 @@ if sys.version_info < (3,):
11171127
if sys.version_info >= (3,):
11181128
def ascii(__o: object) -> str: ...
11191129

1120-
class _SupportsIndex(Protocol):
1121-
def __index__(self) -> int: ...
11221130
def bin(__number: Union[int, _SupportsIndex]) -> str: ...
11231131

11241132
if sys.version_info >= (3, 7):

stdlib/2and3/builtins.pyi

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import sys
1717
if sys.version_info >= (3,):
1818
from typing import SupportsBytes, SupportsRound
1919

20+
if sys.version_info >= (3, 8):
21+
from typing import Literal
22+
else:
23+
from typing_extensions import Literal
24+
2025
_T = TypeVar('_T')
2126
_T_co = TypeVar('_T_co', covariant=True)
2227
_KT = TypeVar('_KT')
@@ -29,6 +34,9 @@ _T4 = TypeVar('_T4')
2934
_T5 = TypeVar('_T5')
3035
_TT = TypeVar('_TT', bound='type')
3136

37+
class _SupportsIndex(Protocol):
38+
def __index__(self) -> int: ...
39+
3240
class object:
3341
__doc__: Optional[str]
3442
__dict__: Dict[str, Any]
@@ -129,10 +137,12 @@ class super(object):
129137

130138
class int:
131139
@overload
132-
def __init__(self, x: Union[Text, bytes, SupportsInt] = ...) -> None: ...
140+
def __init__(self, x: Union[Text, bytes, SupportsInt, _SupportsIndex] = ...) -> None: ...
133141
@overload
134142
def __init__(self, x: Union[Text, bytes, bytearray], base: int) -> None: ...
135143

144+
if sys.version_info >= (3, 8):
145+
def as_integer_ratio(self) -> Tuple[int, Literal[1]]: ...
136146
@property
137147
def real(self) -> int: ...
138148
@property
@@ -209,7 +219,7 @@ class int:
209219
def __index__(self) -> int: ...
210220

211221
class float:
212-
def __init__(self, x: Union[SupportsFloat, Text, bytes, bytearray] = ...) -> None: ...
222+
def __init__(self, x: Union[SupportsFloat, _SupportsIndex, Text, bytes, bytearray] = ...) -> None: ...
213223
def as_integer_ratio(self) -> Tuple[int, int]: ...
214224
def hex(self) -> str: ...
215225
def is_integer(self) -> bool: ...
@@ -271,11 +281,9 @@ class float:
271281

272282
class complex:
273283
@overload
274-
def __init__(self, re: float = ..., im: float = ...) -> None: ...
275-
@overload
276-
def __init__(self, s: str) -> None: ...
284+
def __init__(self, real: float = ..., imag: float = ...) -> None: ...
277285
@overload
278-
def __init__(self, s: SupportsComplex) -> None: ...
286+
def __init__(self, real: Union[str, SupportsComplex, _SupportsIndex]) -> None: ...
279287

280288
@property
281289
def real(self) -> float: ...
@@ -987,6 +995,8 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
987995
def __setitem__(self, k: _KT, v: _VT) -> None: ...
988996
def __delitem__(self, v: _KT) -> None: ...
989997
def __iter__(self) -> Iterator[_KT]: ...
998+
if sys.version_info >= (3, 8):
999+
def __reversed__(self) -> Iterator[_KT]: ...
9901000
def __str__(self) -> str: ...
9911001
__hash__: None # type: ignore
9921002

@@ -1117,8 +1127,6 @@ if sys.version_info < (3,):
11171127
if sys.version_info >= (3,):
11181128
def ascii(__o: object) -> str: ...
11191129

1120-
class _SupportsIndex(Protocol):
1121-
def __index__(self) -> int: ...
11221130
def bin(__number: Union[int, _SupportsIndex]) -> str: ...
11231131

11241132
if sys.version_info >= (3, 7):

stdlib/2and3/datetime.pyi

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import sys
22
from time import struct_time
3-
from typing import (
4-
AnyStr, Optional, SupportsAbs, Tuple, Union, overload,
5-
ClassVar,
6-
)
3+
from typing import AnyStr, Optional, SupportsAbs, Tuple, Union, overload, ClassVar, Type, TypeVar
4+
5+
_S = TypeVar("_S")
76

87
if sys.version_info >= (3,):
98
_Text = str
@@ -68,7 +67,10 @@ class date:
6867
def __lt__(self, other: date) -> bool: ...
6968
def __ge__(self, other: date) -> bool: ...
7069
def __gt__(self, other: date) -> bool: ...
71-
def __add__(self, other: timedelta) -> date: ...
70+
if sys.version_info >= (3, 8):
71+
def __add__(self: _S, other: timedelta) -> _S: ...
72+
else:
73+
def __add__(self, other: timedelta) -> date: ...
7274
@overload
7375
def __sub__(self, other: timedelta) -> date: ...
7476
@overload
@@ -227,8 +229,16 @@ class datetime(date):
227229
def today(cls) -> datetime: ...
228230
@classmethod
229231
def fromordinal(cls, n: int) -> datetime: ...
230-
@classmethod
231-
def now(cls, tz: Optional[_tzinfo] = ...) -> datetime: ...
232+
if sys.version_info >= (3, 8):
233+
@classmethod
234+
def now(cls: Type[_S], tz: Optional[_tzinfo] = ...) -> _S: ...
235+
else:
236+
@overload
237+
@classmethod
238+
def now(cls: Type[_S], tz: None = ...) -> _S: ...
239+
@overload
240+
@classmethod
241+
def now(cls, tz: _tzinfo) -> datetime: ...
232242
@classmethod
233243
def utcnow(cls) -> datetime: ...
234244
if sys.version_info >= (3, 6):
@@ -261,7 +271,9 @@ class datetime(date):
261271
def replace(self, year: int = ..., month: int = ..., day: int = ..., hour: int = ...,
262272
minute: int = ..., second: int = ..., microsecond: int = ..., tzinfo:
263273
Optional[_tzinfo] = ...) -> datetime: ...
264-
if sys.version_info >= (3, 3):
274+
if sys.version_info >= (3, 8):
275+
def astimezone(self: _S, tz: Optional[_tzinfo] = ...) -> _S: ...
276+
elif sys.version_info >= (3, 3):
265277
def astimezone(self, tz: Optional[_tzinfo] = ...) -> datetime: ...
266278
else:
267279
def astimezone(self, tz: _tzinfo) -> datetime: ...
@@ -279,7 +291,10 @@ class datetime(date):
279291
def __lt__(self, other: datetime) -> bool: ... # type: ignore
280292
def __ge__(self, other: datetime) -> bool: ... # type: ignore
281293
def __gt__(self, other: datetime) -> bool: ... # type: ignore
282-
def __add__(self, other: timedelta) -> datetime: ...
294+
if sys.version_info >= (3, 8):
295+
def __add__(self: _S, other: timedelta) -> _S: ...
296+
else:
297+
def __add__(self, other: timedelta) -> datetime: ...
283298
@overload # type: ignore
284299
def __sub__(self, other: datetime) -> timedelta: ...
285300
@overload

stdlib/2and3/fractions.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Note: these stubs are incomplete. The more complex type
55
# signatures are currently omitted. Also see numbers.pyi.
66

7-
from typing import Optional, TypeVar, Union, overload, Any
7+
from typing import Optional, TypeVar, Union, overload, Any, Tuple
88
from numbers import Real, Integral, Rational
99
from decimal import Decimal
1010
import sys
@@ -42,6 +42,8 @@ class Fraction(Rational):
4242
def from_decimal(cls, dec: Decimal) -> Fraction: ...
4343
def limit_denominator(self, max_denominator: int = ...) -> Fraction: ...
4444

45+
if sys.version_info >= (3, 8):
46+
def as_integer_ratio(self) -> Tuple[int, int]: ...
4547
@property
4648
def numerator(self) -> int: ...
4749
@property

stdlib/2and3/shutil.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,17 @@ else:
7272

7373
def ignore_patterns(*patterns: _Path) -> Callable[[Any, List[_AnyStr]], Set[_AnyStr]]: ...
7474

75-
if sys.version_info >= (3,):
75+
if sys.version_info >= (3, 8):
76+
def copytree(
77+
src: _Path,
78+
dst: _Path,
79+
symlinks: bool = ...,
80+
ignore: Union[None, Callable[[str, List[str]], Iterable[str]], Callable[[_Path, List[str]], Iterable[str]]] = ...,
81+
copy_function: Callable[[str, str], None] = ...,
82+
ignore_dangling_symlinks: bool = ...,
83+
dirs_exist_ok: bool = ...,
84+
) -> _PathReturn: ...
85+
elif sys.version_info >= (3,):
7686
def copytree(src: _Path, dst: _Path, symlinks: bool = ...,
7787
ignore: Union[None,
7888
Callable[[str, List[str]], Iterable[str]],

stdlib/3/types.pyi

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,27 @@ class CodeType:
7272
freevars: Tuple[str, ...] = ...,
7373
cellvars: Tuple[str, ...] = ...,
7474
) -> None: ...
75+
if sys.version_info >= (3, 8):
76+
def replace(
77+
self,
78+
*,
79+
co_argcount: int = ...,
80+
co_posonlyargcount: int = ...,
81+
co_kwonlyargcount: int = ...,
82+
co_nlocals: int = ...,
83+
co_stacksize: int = ...,
84+
co_flags: int = ...,
85+
co_firstlineno: int = ...,
86+
co_code: bytes = ...,
87+
co_consts: Tuple[Any, ...] = ...,
88+
co_names: Tuple[str, ...] = ...,
89+
co_varnames: Tuple[str, ...] = ...,
90+
co_freevars: Tuple[str, ...] = ...,
91+
co_cellvars: Tuple[str, ...] = ...,
92+
co_filename: str = ...,
93+
co_name: str = ...,
94+
co_lnotab: bytes = ...,
95+
) -> CodeType: ...
7596

7697
class MappingProxyType(Mapping[_KT, _VT], Generic[_KT, _VT]):
7798
def __init__(self, mapping: Mapping[_KT, _VT]) -> None: ...

stdlib/3/typing.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ class ItemsView(MappingView, AbstractSet[Tuple[_KT_co, _VT_co]], Generic[_KT_co,
339339
def __rand__(self, o: Iterable[_T]) -> Set[_T]: ...
340340
def __contains__(self, o: object) -> bool: ...
341341
def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
342+
if sys.version_info >= (3, 8):
343+
def __reversed__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
342344
def __or__(self, o: Iterable[_T]) -> Set[Union[Tuple[_KT_co, _VT_co], _T]]: ...
343345
def __ror__(self, o: Iterable[_T]) -> Set[Union[Tuple[_KT_co, _VT_co], _T]]: ...
344346
def __sub__(self, o: Iterable[Any]) -> Set[Tuple[_KT_co, _VT_co]]: ...
@@ -351,6 +353,8 @@ class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]):
351353
def __rand__(self, o: Iterable[_T]) -> Set[_T]: ...
352354
def __contains__(self, o: object) -> bool: ...
353355
def __iter__(self) -> Iterator[_KT_co]: ...
356+
if sys.version_info >= (3, 8):
357+
def __reversed__(self) -> Iterator[_KT_co]: ...
354358
def __or__(self, o: Iterable[_T]) -> Set[Union[_KT_co, _T]]: ...
355359
def __ror__(self, o: Iterable[_T]) -> Set[Union[_KT_co, _T]]: ...
356360
def __sub__(self, o: Iterable[Any]) -> Set[_KT_co]: ...
@@ -361,6 +365,8 @@ class KeysView(MappingView, AbstractSet[_KT_co], Generic[_KT_co]):
361365
class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
362366
def __contains__(self, o: object) -> bool: ...
363367
def __iter__(self) -> Iterator[_VT_co]: ...
368+
if sys.version_info >= (3, 8):
369+
def __reversed__(self) -> Iterator[_VT_co]: ...
364370

365371
@runtime_checkable
366372
class ContextManager(Protocol[_T_co]):

0 commit comments

Comments
 (0)