Skip to content

Commit 62bbdf8

Browse files
authored
Add several Python 3.8 annotations (#3347)
1 parent 8ec2570 commit 62bbdf8

11 files changed

Lines changed: 115 additions & 32 deletions

File tree

stdlib/2and3/ctypes/__init__.pyi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ class CDLL(object):
2424
_name: str = ...
2525
_handle: int = ...
2626
_FuncPtr: Type[_FuncPointer] = ...
27-
def __init__(self, name: str, mode: int = ..., handle: Optional[int] = ...,
28-
use_errno: bool = ..., use_last_error: bool = ...) -> None: ...
27+
def __init__(
28+
self,
29+
name: str,
30+
mode: int = ...,
31+
handle: Optional[int] = ...,
32+
use_errno: bool = ...,
33+
use_last_error: bool = ...,
34+
winmode: Optional[int] = ...,
35+
) -> None: ...
2936
def __getattr__(self, name: str) -> _FuncPointer: ...
3037
def __getitem__(self, name: str) -> _FuncPointer: ...
3138
if sys.platform == 'win32':

stdlib/2and3/datetime.pyi

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ class date:
3737
def __init__(self, year: int, month: int, day: int) -> None: ...
3838

3939
@classmethod
40-
def fromtimestamp(cls, t: float) -> date: ...
40+
def fromtimestamp(cls: Type[_S], t: float) -> _S: ...
4141
@classmethod
42-
def today(cls) -> date: ...
42+
def today(cls: Type[_S]) -> _S: ...
4343
@classmethod
44-
def fromordinal(cls, n: int) -> date: ...
44+
def fromordinal(cls: Type[_S], n: int) -> _S: ...
4545
if sys.version_info >= (3, 7):
4646
@classmethod
47-
def fromisoformat(cls, date_string: str) -> date: ...
47+
def fromisoformat(cls: Type[_S], date_string: str) -> _S: ...
48+
if sys.version_info >= (3, 8):
49+
@classmethod
50+
def fromisocalendar(cls: Type[_S], year: int, week: int, day: int) -> _S: ...
4851

4952
@property
5053
def year(self) -> int: ...
@@ -114,7 +117,7 @@ class time:
114117
def isoformat(self) -> str: ...
115118
if sys.version_info >= (3, 7):
116119
@classmethod
117-
def fromisoformat(cls, time_string: str) -> time: ...
120+
def fromisoformat(cls: Type[_S], time_string: str) -> _S: ...
118121
def strftime(self, fmt: _Text) -> str: ...
119122
if sys.version_info >= (3,):
120123
def __format__(self, fmt: str) -> str: ...
@@ -222,13 +225,13 @@ class datetime(date):
222225
def fold(self) -> int: ...
223226

224227
@classmethod
225-
def fromtimestamp(cls, t: float, tz: Optional[_tzinfo] = ...) -> datetime: ...
228+
def fromtimestamp(cls: Type[_S], t: float, tz: Optional[_tzinfo] = ...) -> _S: ...
226229
@classmethod
227-
def utcfromtimestamp(cls, t: float) -> datetime: ...
230+
def utcfromtimestamp(cls: Type[_S], t: float) -> _S: ...
228231
@classmethod
229-
def today(cls) -> datetime: ...
232+
def today(cls: Type[_S]) -> _S: ...
230233
@classmethod
231-
def fromordinal(cls, n: int) -> datetime: ...
234+
def fromordinal(cls: Type[_S], n: int) -> _S: ...
232235
if sys.version_info >= (3, 8):
233236
@classmethod
234237
def now(cls: Type[_S], tz: Optional[_tzinfo] = ...) -> _S: ...
@@ -240,7 +243,7 @@ class datetime(date):
240243
@classmethod
241244
def now(cls, tz: _tzinfo) -> datetime: ...
242245
@classmethod
243-
def utcnow(cls) -> datetime: ...
246+
def utcnow(cls: Type[_S]) -> _S: ...
244247
if sys.version_info >= (3, 6):
245248
@classmethod
246249
def combine(cls, date: _date, time: _time, tzinfo: Optional[_tzinfo] = ...) -> datetime: ...
@@ -249,7 +252,7 @@ class datetime(date):
249252
def combine(cls, date: _date, time: _time) -> datetime: ...
250253
if sys.version_info >= (3, 7):
251254
@classmethod
252-
def fromisoformat(cls, date_string: str) -> datetime: ...
255+
def fromisoformat(cls: Type[_S], date_string: str) -> _S: ...
253256
def strftime(self, fmt: _Text) -> str: ...
254257
if sys.version_info >= (3,):
255258
def __format__(self, fmt: str) -> str: ...

stdlib/2and3/math.pyi

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Stubs for math
22
# See: http://docs.python.org/2/library/math.html
33

4-
from typing import Tuple, Iterable, SupportsFloat, SupportsInt
4+
from typing import Tuple, Iterable, SupportsFloat, SupportsInt, overload
55

66
import sys
77

@@ -28,6 +28,8 @@ def copysign(x: SupportsFloat, y: SupportsFloat) -> float: ...
2828
def cos(x: SupportsFloat) -> float: ...
2929
def cosh(x: SupportsFloat) -> float: ...
3030
def degrees(x: SupportsFloat) -> float: ...
31+
if sys.version_info >= (3, 8):
32+
def dist(__p: Iterable[SupportsFloat], __q: Iterable[SupportsFloat]) -> float: ...
3133
def erf(x: SupportsFloat) -> float: ...
3234
def erfc(x: SupportsFloat) -> float: ...
3335
def exp(x: SupportsFloat) -> float: ...
@@ -44,13 +46,18 @@ def fsum(iterable: Iterable[float]) -> float: ...
4446
def gamma(x: SupportsFloat) -> float: ...
4547
if sys.version_info >= (3, 5):
4648
def gcd(a: int, b: int) -> int: ...
47-
def hypot(x: SupportsFloat, y: SupportsFloat) -> float: ...
49+
if sys.version_info >= (3, 8):
50+
def hypot(*coordinates: SupportsFloat) -> float: ...
51+
else:
52+
def hypot(__x: SupportsFloat, __y: SupportsFloat) -> float: ...
4853
if sys.version_info >= (3, 5):
4954
def isclose(a: SupportsFloat, b: SupportsFloat, rel_tol: SupportsFloat = ..., abs_tol: SupportsFloat = ...) -> bool: ...
5055
def isinf(x: SupportsFloat) -> bool: ...
5156
if sys.version_info >= (3,):
5257
def isfinite(x: SupportsFloat) -> bool: ...
5358
def isnan(x: SupportsFloat) -> bool: ...
59+
if sys.version_info >= (3, 8):
60+
def isqrt(__n: int) -> int: ...
5461
def ldexp(x: SupportsFloat, i: int) -> float: ...
5562
def lgamma(x: SupportsFloat) -> float: ...
5663
def log(x: SupportsFloat, base: SupportsFloat = ...) -> float: ...
@@ -60,6 +67,11 @@ if sys.version_info >= (3, 3):
6067
def log2(x: SupportsFloat) -> float: ...
6168
def modf(x: SupportsFloat) -> Tuple[float, float]: ...
6269
def pow(x: SupportsFloat, y: SupportsFloat) -> float: ...
70+
if sys.version_info >= (3, 8):
71+
@overload
72+
def prod(__iterable: Iterable[int], *, start: int = ...) -> int: ... # type: ignore
73+
@overload
74+
def prod(__iterable: Iterable[SupportsFloat], *, start: SupportsFloat = ...) -> float: ...
6375
def radians(x: SupportsFloat) -> float: ...
6476
if sys.version_info >= (3, 7):
6577
def remainder(x: SupportsFloat, y: SupportsFloat) -> float: ...

stdlib/2and3/mmap.pyi

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class _mmap(Generic[AnyStr]):
5151
if sys.version_info >= (3,):
5252
class mmap(_mmap[bytes], ContextManager[mmap], Iterable[bytes], Sized):
5353
closed: bool
54+
if sys.version_info >= (3, 8):
55+
def madvise(self, option: int, start: int = ..., length: int = ...) -> None: ...
5456
def rfind(self, sub: bytes, start: int = ..., stop: int = ...) -> int: ...
5557
@overload
5658
def __getitem__(self, index: int) -> int: ...
@@ -71,3 +73,27 @@ else:
7173
def __getslice__(self, i: Optional[int], j: Optional[int]) -> bytes: ...
7274
def __delitem__(self, index: Union[int, slice]) -> None: ...
7375
def __setitem__(self, index: Union[int, slice], object: bytes) -> None: ...
76+
77+
if sys.version_info >= (3, 8):
78+
MADV_NORMAL: int
79+
MADV_RANDOM: int
80+
MADV_SEQUENTIAL: int
81+
MADV_WILLNEED: int
82+
MADV_DONTNEED: int
83+
MADV_REMOVE: int
84+
MADV_DONTFORK: int
85+
MADV_DOFORK: int
86+
MADV_HWPOISON: int
87+
MADV_MERGEABLE: int
88+
MADV_UNMERGEABLE: int
89+
MADV_SOFT_OFFLINE: int
90+
MADV_HUGEPAGE: int
91+
MADV_NOHUGEPAGE: int
92+
MADV_DONTDUMP: int
93+
MADV_DODUMP: int
94+
MADV_FREE: int
95+
MADV_NOSYNC: int
96+
MADV_AUTOSYNC: int
97+
MADV_NOCORE: int
98+
MADV_CORE: int
99+
MADV_PROTECT: int

stdlib/3/_ast.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import typing
33
from typing import Any, Optional, ClassVar
44

55
PyCF_ONLY_AST: int
6+
if sys.version_info >= (3, 8):
7+
PyCF_TYPE_COMMENTS: int
8+
PyCF_ALLOW_TOP_LEVEL_AWAIT: int
69

710
_identifier = str
811

stdlib/3/ast.pyi

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sys
33
# from _ast below when loaded in an unorthodox way by the Dropbox
44
# internal Bazel integration.
55
import typing as _typing
6-
from typing import overload, Any, Iterator, Optional, Union, TypeVar
6+
from typing import Any, Iterator, Optional, Tuple, TypeVar, Union, overload
77

88
# The same unorthodox Bazel integration causes issues with sys, which
99
# is imported in both modules. unfortunately we can't just rename sys,
@@ -16,27 +16,36 @@ if sys.version_info >= (3, 8):
1616
else:
1717
from typing_extensions import Literal
1818

19-
class NodeVisitor():
19+
class NodeVisitor:
2020
def visit(self, node: AST) -> Any: ...
2121
def generic_visit(self, node: AST) -> Any: ...
2222

2323
class NodeTransformer(NodeVisitor):
2424
def generic_visit(self, node: AST) -> Optional[AST]: ...
2525

26-
_T = TypeVar('_T', bound=AST)
26+
_T = TypeVar("_T", bound=AST)
2727

2828
if sys.version_info >= (3, 8):
2929
@overload
30-
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...,
31-
type_comments: bool = ..., feature_version: int = ...) -> Module: ...
32-
30+
def parse(
31+
source: Union[str, bytes],
32+
filename: Union[str, bytes] = ...,
33+
mode: Literal["exec"] = ...,
34+
type_comments: bool = ...,
35+
feature_version: Union[None, int, Tuple[int, int]] = ...,
36+
) -> Module: ...
3337
@overload
34-
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...,
35-
type_comments: bool = ..., feature_version: int = ...) -> AST: ...
38+
def parse(
39+
source: Union[str, bytes],
40+
filename: Union[str, bytes] = ...,
41+
mode: str = ...,
42+
type_comments: bool = ...,
43+
feature_version: Union[None, int, Tuple[int, int]] = ...,
44+
) -> AST: ...
45+
3646
else:
3747
@overload
3848
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: Literal["exec"] = ...) -> Module: ...
39-
4049
@overload
4150
def parse(source: Union[str, bytes], filename: Union[str, bytes] = ..., mode: str = ...) -> AST: ...
4251

@@ -48,4 +57,5 @@ def increment_lineno(node: _T, n: int = ...) -> _T: ...
4857
def iter_child_nodes(node: AST) -> Iterator[AST]: ...
4958
def iter_fields(node: AST) -> Iterator[_typing.Tuple[str, Any]]: ...
5059
def literal_eval(node_or_string: Union[str, AST]) -> Any: ...
60+
def get_source_segment(source: str, node: AST, *, padded: bool = ...) -> Optional[str]: ...
5161
def walk(node: AST) -> Iterator[AST]: ...

stdlib/3/functools.pyi

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import sys
12
from typing import Any, Callable, Generic, Dict, Iterable, Mapping, Optional, Sequence, Tuple, Type, TypeVar, NamedTuple, Union, overload
23

34
_AnyCallable = Callable[..., Any]
45

56
_T = TypeVar("_T")
67
_S = TypeVar("_S")
8+
79
@overload
810
def reduce(function: Callable[[_T, _S], _T],
911
sequence: Iterable[_S], initial: _T) -> _T: ...
@@ -25,10 +27,13 @@ class _lru_cache_wrapper(Generic[_T]):
2527
def cache_info(self) -> _CacheInfo: ...
2628
def cache_clear(self) -> None: ...
2729

28-
class lru_cache():
29-
def __init__(self, maxsize: Optional[int] = ..., typed: bool = ...) -> None: ...
30-
def __call__(self, f: Callable[..., _T]) -> _lru_cache_wrapper[_T]: ...
31-
30+
if sys.version_info >= (3, 8):
31+
@overload
32+
def lru_cache(maxsize: Optional[int] = ..., typed: bool = ...) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ...
33+
@overload
34+
def lru_cache(maxsize: Callable[..., _T], typed: bool = ...) -> _lru_cache_wrapper[_T]: ...
35+
else:
36+
def lru_cache(maxsize: Optional[int] = ..., typed: bool = ...) -> Callable[[Callable[..., _T]], _lru_cache_wrapper[_T]]: ...
3237

3338
WRAPPER_ASSIGNMENTS: Sequence[str]
3439
WRAPPER_UPDATES: Sequence[str]

stdlib/3/gc.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Stubs for gc
22

3-
from typing import Any, Dict, List, Tuple
3+
import sys
4+
from typing import Any, Dict, List, Optional, Tuple
45

56

67
DEBUG_COLLECTABLE: int
@@ -16,7 +17,10 @@ def disable() -> None: ...
1617
def enable() -> None: ...
1718
def get_count() -> Tuple[int, int, int]: ...
1819
def get_debug() -> int: ...
19-
def get_objects() -> List[Any]: ...
20+
if sys.version_info >= (3, 8):
21+
def get_objects(generation: Optional[int] = ...) -> List[Any]: ...
22+
else:
23+
def get_objects() -> List[Any]: ...
2024
def get_referents(*objs: Any) -> List[Any]: ...
2125
def get_referrers(*objs: Any) -> List[Any]: ...
2226
def get_stats() -> List[Dict[str, Any]]: ...

stdlib/3/gettext.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class NullTranslations:
1414
def lgettext(self, message: str) -> str: ...
1515
def ngettext(self, singular: str, plural: str, n: int) -> str: ...
1616
def lngettext(self, singular: str, plural: str, n: int) -> str: ...
17+
def pgettext(self, context: str, message: str) -> str: ...
18+
def npgettext(self, context: str, msgid1: str, msgid2: str, n: int) -> str: ...
1719
def info(self) -> Any: ...
1820
def charset(self) -> Any: ...
1921
def output_charset(self) -> Any: ...
@@ -52,5 +54,9 @@ def gettext(message: str) -> str: ...
5254
def lgettext(message: str) -> str: ...
5355
def ngettext(singular: str, plural: str, n: int) -> str: ...
5456
def lngettext(singular: str, plural: str, n: int) -> str: ...
57+
def pgettext(context: str, message: str) -> str: ...
58+
def dpgettext(domain: str, context: str, message: str) -> str: ...
59+
def npgettext(context: str, msgid1: str, msgid2: str, n: int) -> str: ...
60+
def dnpgettext(domain: str, context: str, msgid1: str, msgid2: str, n: int) -> str: ...
5561

5662
Catalog = translation

stdlib/3/gzip.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Any, IO, Optional
22
from os.path import _PathType
33
import _compression
4+
import sys
45
import zlib
56

67
def open(filename, mode: str = ..., compresslevel: int = ..., encoding: Optional[str] = ..., errors: Optional[str] = ..., newline: Optional[str] = ...) -> IO[Any]: ...
@@ -45,5 +46,8 @@ class _GzipReader(_compression.DecompressReader):
4546
def __init__(self, fp: IO[bytes]) -> None: ...
4647
def read(self, size: int = ...) -> bytes: ...
4748

48-
def compress(data, compresslevel: int = ...) -> bytes: ...
49+
if sys.version_info >= (3, 8):
50+
def compress(data, compresslevel: int = ..., *, mtime: Optional[float] = ...) -> bytes: ...
51+
else:
52+
def compress(data, compresslevel: int = ...) -> bytes: ...
4953
def decompress(data: bytes) -> bytes: ...

0 commit comments

Comments
 (0)