Skip to content

Commit 4996d57

Browse files
authored
Sync typeshed (#11600)
Source commit: python/typeshed@2445edd
1 parent ce3975d commit 4996d57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1093
-752
lines changed

mypy/typeshed/stdlib/@python2/__builtin__.pyi

+8-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class type(object):
121121
def __new__(cls, name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> type: ...
122122
def __call__(self, *args: Any, **kwds: Any) -> Any: ...
123123
def __subclasses__(self: _TT) -> List[_TT]: ...
124-
# Note: the documentation doesnt specify what the return type is, the standard
124+
# Note: the documentation doesn't specify what the return type is, the standard
125125
# implementation seems to be returning a list.
126126
def mro(self) -> List[type]: ...
127127
def __instancecheck__(self, instance: Any) -> bool: ...
@@ -850,13 +850,18 @@ def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicod
850850
@overload
851851
def getattr(__o: Any, name: Text) -> Any: ...
852852

853-
# While technically covered by the last overload, spelling out the types for None and bool
854-
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
853+
# While technically covered by the last overload, spelling out the types for None, bool
854+
# and basic containers help mypy out in some tricky situations involving type context
855+
# (aka bidirectional inference)
855856
@overload
856857
def getattr(__o: Any, name: Text, __default: None) -> Any | None: ...
857858
@overload
858859
def getattr(__o: Any, name: Text, __default: bool) -> Any | bool: ...
859860
@overload
861+
def getattr(__o: object, name: str, __default: list[Any]) -> Any | list[Any]: ...
862+
@overload
863+
def getattr(__o: object, name: str, __default: dict[Any, Any]) -> Any | dict[Any, Any]: ...
864+
@overload
860865
def getattr(__o: Any, name: Text, __default: _T) -> Any | _T: ...
861866
def globals() -> Dict[str, Any]: ...
862867
def hasattr(__obj: Any, __name: Text) -> bool: ...

mypy/typeshed/stdlib/@python2/ast.pyi

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
# from _ast below when loaded in an unorthodox way by the Dropbox
55
# internal Bazel integration.
66
import typing as _typing
7-
from typing import Any, Iterator
8-
97
from _ast import *
108
from _ast import AST, Module
9+
from typing import Any, Iterator
1110

1211
def parse(source: str | unicode, filename: str | unicode = ..., mode: str | unicode = ...) -> Module: ...
1312
def copy_location(new_node: AST, old_node: AST) -> AST: ...

mypy/typeshed/stdlib/@python2/builtins.pyi

+8-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class type(object):
121121
def __new__(cls, name: str, bases: Tuple[type, ...], namespace: Dict[str, Any]) -> type: ...
122122
def __call__(self, *args: Any, **kwds: Any) -> Any: ...
123123
def __subclasses__(self: _TT) -> List[_TT]: ...
124-
# Note: the documentation doesnt specify what the return type is, the standard
124+
# Note: the documentation doesn't specify what the return type is, the standard
125125
# implementation seems to be returning a list.
126126
def mro(self) -> List[type]: ...
127127
def __instancecheck__(self, instance: Any) -> bool: ...
@@ -850,13 +850,18 @@ def format(__value: object, __format_spec: str = ...) -> str: ... # TODO unicod
850850
@overload
851851
def getattr(__o: Any, name: Text) -> Any: ...
852852

853-
# While technically covered by the last overload, spelling out the types for None and bool
854-
# help mypy out in some tricky situations involving type context (aka bidirectional inference)
853+
# While technically covered by the last overload, spelling out the types for None, bool
854+
# and basic containers help mypy out in some tricky situations involving type context
855+
# (aka bidirectional inference)
855856
@overload
856857
def getattr(__o: Any, name: Text, __default: None) -> Any | None: ...
857858
@overload
858859
def getattr(__o: Any, name: Text, __default: bool) -> Any | bool: ...
859860
@overload
861+
def getattr(__o: object, name: str, __default: list[Any]) -> Any | list[Any]: ...
862+
@overload
863+
def getattr(__o: object, name: str, __default: dict[Any, Any]) -> Any | dict[Any, Any]: ...
864+
@overload
860865
def getattr(__o: Any, name: Text, __default: _T) -> Any | _T: ...
861866
def globals() -> Dict[str, Any]: ...
862867
def hasattr(__obj: Any, __name: Text) -> bool: ...

mypy/typeshed/stdlib/@python2/ctypes/__init__.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def POINTER(type: Type[_CT]) -> Type[pointer[_CT]]: ...
166166
# ctypes._Pointer in that it is the base class for all pointer types. Unlike the real _Pointer,
167167
# it can be instantiated directly (to mimic the behavior of the real pointer function).
168168
class pointer(Generic[_CT], _PointerLike, _CData):
169-
_type_: ClassVar[Type[_CT]] = ...
169+
_type_: Type[_CT] = ...
170170
contents: _CT = ...
171171
def __init__(self, arg: _CT = ...) -> None: ...
172172
@overload
@@ -262,8 +262,8 @@ class BigEndianStructure(Structure): ...
262262
class LittleEndianStructure(Structure): ...
263263

264264
class Array(Generic[_CT], _CData):
265-
_length_: ClassVar[int] = ...
266-
_type_: ClassVar[Type[_CT]] = ...
265+
_length_: int = ...
266+
_type_: Type[_CT] = ...
267267
raw: bytes = ... # Note: only available if _CT == c_char
268268
value: Any = ... # Note: bytes if _CT == c_char, Text if _CT == c_wchar, unavailable otherwise
269269
# TODO These methods cannot be annotated correctly at the moment.

mypy/typeshed/stdlib/@python2/logging/__init__.pyi

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import threading
2-
from _typeshed import StrPath
2+
from _typeshed import StrPath, SupportsWrite
33
from time import struct_time
44
from types import FrameType, TracebackType
5-
from typing import IO, Any, Callable, Dict, List, Mapping, MutableMapping, Optional, Sequence, Text, Tuple, Union, overload
5+
from typing import (
6+
IO,
7+
Any,
8+
Callable,
9+
Dict,
10+
Generic,
11+
List,
12+
Mapping,
13+
MutableMapping,
14+
Optional,
15+
Sequence,
16+
Text,
17+
Tuple,
18+
TypeVar,
19+
Union,
20+
overload,
21+
)
622

723
_SysExcInfoType = Union[Tuple[type, BaseException, Optional[TracebackType]], Tuple[None, None, None]]
824
_ExcInfoType = Union[None, bool, _SysExcInfoType]
@@ -159,10 +175,12 @@ class LogRecord:
159175
) -> None: ...
160176
def getMessage(self) -> str: ...
161177

162-
class LoggerAdapter:
163-
logger: Logger
178+
_L = TypeVar("_L", Logger, LoggerAdapter[Logger], LoggerAdapter[Any])
179+
180+
class LoggerAdapter(Generic[_L]):
181+
logger: _L
164182
extra: Mapping[str, Any]
165-
def __init__(self, logger: Logger, extra: Mapping[str, Any]) -> None: ...
183+
def __init__(self, logger: _L, extra: Mapping[str, Any]) -> None: ...
166184
def process(self, msg: Any, kwargs: MutableMapping[str, Any]) -> Tuple[Any, MutableMapping[str, Any]]: ...
167185
def debug(
168186
self, msg: Any, *args: Any, exc_info: _ExcInfoType = ..., extra: Dict[str, Any] | None = ..., **kwargs: Any
@@ -227,9 +245,14 @@ def shutdown(handlerList: Sequence[Any] = ...) -> None: ... # handlerList is un
227245
def setLoggerClass(klass: type) -> None: ...
228246
def captureWarnings(capture: bool) -> None: ...
229247

230-
class StreamHandler(Handler):
231-
stream: IO[str] # undocumented
232-
def __init__(self, stream: IO[str] | None = ...) -> None: ...
248+
_StreamT = TypeVar("_StreamT", bound=SupportsWrite[str])
249+
250+
class StreamHandler(Handler, Generic[_StreamT]):
251+
stream: _StreamT # undocumented
252+
@overload
253+
def __init__(self: StreamHandler[IO[str]], stream: None = ...) -> None: ...
254+
@overload
255+
def __init__(self: StreamHandler[_StreamT], stream: _StreamT) -> None: ...
233256

234257
class FileHandler(StreamHandler):
235258
baseFilename: str # undocumented

mypy/typeshed/stdlib/@python2/warnings.pyi

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
from _warnings import warn as warn, warn_explicit as warn_explicit
12
from types import ModuleType, TracebackType
23
from typing import List, TextIO, Type, overload
34
from typing_extensions import Literal
45

5-
from _warnings import warn as warn, warn_explicit as warn_explicit
6-
76
def showwarning(
87
message: Warning | str, category: Type[Warning], filename: str, lineno: int, file: TextIO | None = ..., line: str | None = ...
98
) -> None: ...

mypy/typeshed/stdlib/_collections_abc.pyi

+23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
from types import MappingProxyType
13
from typing import (
24
AbstractSet as Set,
35
AsyncGenerator as AsyncGenerator,
@@ -10,6 +12,7 @@ from typing import (
1012
Container as Container,
1113
Coroutine as Coroutine,
1214
Generator as Generator,
15+
Generic,
1316
Hashable as Hashable,
1417
ItemsView as ItemsView,
1518
Iterable as Iterable,
@@ -23,8 +26,10 @@ from typing import (
2326
Reversible as Reversible,
2427
Sequence as Sequence,
2528
Sized as Sized,
29+
TypeVar,
2630
ValuesView as ValuesView,
2731
)
32+
from typing_extensions import final
2833

2934
__all__ = [
3035
"Awaitable",
@@ -53,3 +58,21 @@ __all__ = [
5358
"MutableSequence",
5459
"ByteString",
5560
]
61+
62+
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
63+
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
64+
65+
@final
66+
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
67+
if sys.version_info >= (3, 10):
68+
mapping: MappingProxyType[_KT_co, _VT_co]
69+
70+
@final
71+
class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented
72+
if sys.version_info >= (3, 10):
73+
mapping: MappingProxyType[_KT_co, _VT_co]
74+
75+
@final
76+
class dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): # undocumented
77+
if sys.version_info >= (3, 10):
78+
mapping: MappingProxyType[_KT_co, _VT_co]

mypy/typeshed/stdlib/_thread.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import sys
22
from threading import Thread
33
from types import TracebackType
44
from typing import Any, Callable, NoReturn, Optional, Tuple, Type
5+
from typing_extensions import final
56

67
error = RuntimeError
78

89
def _count() -> int: ...
910

1011
_dangling: Any
1112

13+
@final
1214
class LockType:
1315
def acquire(self, blocking: bool = ..., timeout: float = ...) -> bool: ...
1416
def release(self) -> None: ...
@@ -29,6 +31,7 @@ TIMEOUT_MAX: float
2931

3032
if sys.version_info >= (3, 8):
3133
def get_native_id() -> int: ... # only available on some platforms
34+
@final
3235
class _ExceptHookArgs(Tuple[Type[BaseException], Optional[BaseException], Optional[TracebackType], Optional[Thread]]):
3336
@property
3437
def exc_type(self) -> Type[BaseException]: ...

mypy/typeshed/stdlib/_tkinter.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Any
2-
from typing_extensions import Literal
2+
from typing_extensions import Literal, final
33

44
# _tkinter is meant to be only used internally by tkinter, but some tkinter
55
# functions e.g. return _tkinter.Tcl_Obj objects. Tcl_Obj represents a Tcl
@@ -14,6 +14,7 @@ from typing_extensions import Literal
1414
# >>> text.tag_add('foo', '1.0', 'end')
1515
# >>> text.tag_ranges('foo')
1616
# (<textindex object: '1.0'>, <textindex object: '2.0'>)
17+
@final
1718
class Tcl_Obj:
1819
string: str # str(tclobj) returns this
1920
typename: str
@@ -37,6 +38,7 @@ class TclError(Exception): ...
3738
#
3839
# eval always returns str because _tkinter_tkapp_eval_impl in _tkinter.c calls
3940
# Tkapp_UnicodeResult, and it returns a string when it succeeds.
41+
@final
4042
class TkappType:
4143
# Please keep in sync with tkinter.Tk
4244
def call(self, __command: Any, *args: Any) -> Any: ...

mypy/typeshed/stdlib/_typeshed/__init__.pyi

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class SupportsLessThan(Protocol):
4040

4141
SupportsLessThanT = TypeVar("SupportsLessThanT", bound=SupportsLessThan) # noqa: Y001
4242

43+
class SupportsGreaterThan(Protocol):
44+
def __gt__(self, __other: Any) -> bool: ...
45+
46+
SupportsGreaterThanT = TypeVar("SupportsGreaterThanT", bound=SupportsGreaterThan) # noqa: Y001
47+
4348
class SupportsDivMod(Protocol[_T_contra, _T_co]):
4449
def __divmod__(self, __other: _T_contra) -> _T_co: ...
4550

mypy/typeshed/stdlib/_weakref.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import sys
22
from typing import Any, Callable, Generic, TypeVar, overload
3+
from typing_extensions import final
34

45
if sys.version_info >= (3, 9):
56
from types import GenericAlias
67

78
_C = TypeVar("_C", bound=Callable[..., Any])
89
_T = TypeVar("_T")
910

11+
@final
1012
class CallableProxyType(Generic[_C]): # "weakcallableproxy"
1113
def __getattr__(self, attr: str) -> Any: ...
1214

15+
@final
1316
class ProxyType(Generic[_T]): # "weakproxy"
1417
def __getattr__(self, attr: str) -> Any: ...
1518

mypy/typeshed/stdlib/_winapi.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from typing import Any, NoReturn, Sequence, overload
3-
from typing_extensions import Literal
3+
from typing_extensions import Literal, final
44

55
CREATE_NEW_CONSOLE: int
66
CREATE_NEW_PROCESS_GROUP: int
@@ -126,7 +126,7 @@ def WriteFile(handle: int, buffer: bytes, overlapped: Literal[True]) -> tuple[Ov
126126
def WriteFile(handle: int, buffer: bytes, overlapped: Literal[False] = ...) -> tuple[int, int]: ...
127127
@overload
128128
def WriteFile(handle: int, buffer: bytes, overlapped: int | bool) -> tuple[Any, int]: ...
129-
129+
@final
130130
class Overlapped:
131131
event: int
132132
def GetOverlappedResult(self, __wait: bool) -> tuple[int, int]: ...

mypy/typeshed/stdlib/array.pyi

+17-17
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class array(MutableSequence[_T], Generic[_T]):
1515
typecode: _TypeCode
1616
itemsize: int
1717
@overload
18-
def __init__(self: array[int], typecode: _IntTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
18+
def __init__(self: array[int], __typecode: _IntTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
1919
@overload
20-
def __init__(self: array[float], typecode: _FloatTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
20+
def __init__(self: array[float], __typecode: _FloatTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
2121
@overload
22-
def __init__(self: array[str], typecode: _UnicodeTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
22+
def __init__(self: array[str], __typecode: _UnicodeTypeCode, __initializer: bytes | Iterable[_T] = ...) -> None: ...
2323
@overload
2424
def __init__(self, typecode: str, __initializer: bytes | Iterable[_T] = ...) -> None: ...
2525
def append(self, __v: _T) -> None: ...
@@ -48,22 +48,22 @@ class array(MutableSequence[_T], Generic[_T]):
4848
def tostring(self) -> bytes: ...
4949
def __len__(self) -> int: ...
5050
@overload
51-
def __getitem__(self, i: int) -> _T: ...
51+
def __getitem__(self, __i: int) -> _T: ...
5252
@overload
53-
def __getitem__(self, s: slice) -> array[_T]: ...
53+
def __getitem__(self, __s: slice) -> array[_T]: ...
5454
@overload # type: ignore # Overrides MutableSequence
55-
def __setitem__(self, i: int, o: _T) -> None: ...
55+
def __setitem__(self, __i: int, __o: _T) -> None: ...
5656
@overload
57-
def __setitem__(self, s: slice, o: array[_T]) -> None: ...
58-
def __delitem__(self, i: int | slice) -> None: ...
59-
def __add__(self, x: array[_T]) -> array[_T]: ...
60-
def __ge__(self, other: array[_T]) -> bool: ...
61-
def __gt__(self, other: array[_T]) -> bool: ...
62-
def __iadd__(self, x: array[_T]) -> array[_T]: ... # type: ignore # Overrides MutableSequence
63-
def __imul__(self, n: int) -> array[_T]: ...
64-
def __le__(self, other: array[_T]) -> bool: ...
65-
def __lt__(self, other: array[_T]) -> bool: ...
66-
def __mul__(self, n: int) -> array[_T]: ...
67-
def __rmul__(self, n: int) -> array[_T]: ...
57+
def __setitem__(self, __s: slice, __o: array[_T]) -> None: ...
58+
def __delitem__(self, __i: int | slice) -> None: ...
59+
def __add__(self, __x: array[_T]) -> array[_T]: ...
60+
def __ge__(self, __other: array[_T]) -> bool: ...
61+
def __gt__(self, __other: array[_T]) -> bool: ...
62+
def __iadd__(self, __x: array[_T]) -> array[_T]: ... # type: ignore # Overrides MutableSequence
63+
def __imul__(self, __n: int) -> array[_T]: ...
64+
def __le__(self, __other: array[_T]) -> bool: ...
65+
def __lt__(self, __other: array[_T]) -> bool: ...
66+
def __mul__(self, __n: int) -> array[_T]: ...
67+
def __rmul__(self, __n: int) -> array[_T]: ...
6868

6969
ArrayType = array

mypy/typeshed/stdlib/ast.pyi

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
# sys.
99
import sys
1010
import typing as _typing
11+
from _ast import * # type: ignore
1112
from typing import Any, Iterator, TypeVar, overload
1213
from typing_extensions import Literal
1314

14-
from _ast import * # type: ignore
15-
1615
if sys.version_info >= (3, 8):
1716
class Num(Constant):
1817
value: complex

mypy/typeshed/stdlib/asyncio/base_subprocess.pyi

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import subprocess
2-
from typing import IO, Any, Callable, Deque, Optional, Sequence, Tuple, Union
2+
from collections import deque
3+
from typing import IO, Any, Callable, Optional, Sequence, Tuple, Union
34

45
from . import events, futures, protocols, transports
56

@@ -14,7 +15,7 @@ class BaseSubprocessTransport(transports.SubprocessTransport):
1415
_pid: int | None # undocumented
1516
_returncode: int | None # undocumented
1617
_exit_waiters: list[futures.Future[Any]] # undocumented
17-
_pending_calls: Deque[tuple[Callable[..., Any], Tuple[Any, ...]]] # undocumented
18+
_pending_calls: deque[tuple[Callable[..., Any], Tuple[Any, ...]]] # undocumented
1819
_pipes: dict[int, _File] # undocumented
1920
_finished: bool # undocumented
2021
def __init__(

0 commit comments

Comments
 (0)