Skip to content

Commit cd895ce

Browse files
Sync typeshed (#17201)
Source commit: python/typeshed@f244be9
1 parent ba6febc commit cd895ce

31 files changed

+608
-544
lines changed

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ AnyStr_co = TypeVar("AnyStr_co", str, bytes, covariant=True) # noqa: Y001
4747
# isn't possible or a type is already partially known. In cases like these,
4848
# use Incomplete instead of Any as a marker. For example, use
4949
# "Incomplete | None" instead of "Any | None".
50-
Incomplete: TypeAlias = Any
50+
Incomplete: TypeAlias = Any # stable
5151

5252
# To describe a function parameter that is unused and will work with anything.
53-
Unused: TypeAlias = object
53+
Unused: TypeAlias = object # stable
54+
55+
# Marker for return types that include None, but where forcing the user to
56+
# check for None can be detrimental. Sometimes called "the Any trick". See
57+
# CONTRIBUTING.md for more information.
58+
MaybeNone: TypeAlias = Any # stable
5459

5560
# Used to mark arguments that default to a sentinel value. This prevents
5661
# stubtest from complaining about the default value not matching.
@@ -146,13 +151,22 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
146151
def keys(self) -> Iterable[_KT]: ...
147152
def __getitem__(self, key: _KT, /) -> _VT_co: ...
148153

149-
# stable
154+
# This protocol is currently under discussion. Use SupportsContainsAndGetItem
155+
# instead, if you require the __contains__ method.
156+
# See https://github.com/python/typeshed/issues/11822.
150157
class SupportsGetItem(Protocol[_KT_contra, _VT_co]):
151158
def __contains__(self, x: Any, /) -> bool: ...
152159
def __getitem__(self, key: _KT_contra, /) -> _VT_co: ...
153160

154161
# stable
155-
class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, _VT]):
162+
class SupportsContainsAndGetItem(Protocol[_KT_contra, _VT_co]):
163+
def __contains__(self, x: Any, /) -> bool: ...
164+
def __getitem__(self, key: _KT_contra, /) -> _VT_co: ...
165+
166+
# stable
167+
class SupportsItemAccess(Protocol[_KT_contra, _VT]):
168+
def __contains__(self, x: Any, /) -> bool: ...
169+
def __getitem__(self, key: _KT_contra, /) -> _VT: ...
156170
def __setitem__(self, key: _KT_contra, value: _VT, /) -> None: ...
157171
def __delitem__(self, key: _KT_contra, /) -> None: ...
158172

mypy/typeshed/stdlib/asyncio/constants.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ if sys.version_info >= (3, 12):
1515
THREAD_JOIN_TIMEOUT: Literal[300]
1616

1717
class _SendfileMode(enum.Enum):
18-
UNSUPPORTED: int
19-
TRY_NATIVE: int
20-
FALLBACK: int
18+
UNSUPPORTED = 1
19+
TRY_NATIVE = 2
20+
FALLBACK = 3

mypy/typeshed/stdlib/asyncio/coroutines.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from collections.abc import Awaitable, Callable, Coroutine
33
from typing import Any, TypeVar, overload
4-
from typing_extensions import ParamSpec, TypeGuard
4+
from typing_extensions import ParamSpec, TypeGuard, TypeIs
55

66
if sys.version_info >= (3, 11):
77
__all__ = ("iscoroutinefunction", "iscoroutine")
@@ -23,4 +23,4 @@ def iscoroutinefunction(func: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable
2323
def iscoroutinefunction(func: Callable[_P, object]) -> TypeGuard[Callable[_P, Coroutine[Any, Any, Any]]]: ...
2424
@overload
2525
def iscoroutinefunction(func: object) -> TypeGuard[Callable[..., Coroutine[Any, Any, Any]]]: ...
26-
def iscoroutine(obj: object) -> TypeGuard[Coroutine[Any, Any, Any]]: ...
26+
def iscoroutine(obj: object) -> TypeIs[Coroutine[Any, Any, Any]]: ...

mypy/typeshed/stdlib/asyncio/futures.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from collections.abc import Awaitable, Callable, Generator, Iterable
33
from concurrent.futures._base import Future as _ConcurrentFuture
44
from contextvars import Context
55
from typing import Any, Literal, TypeVar
6-
from typing_extensions import Self, TypeGuard
6+
from typing_extensions import Self, TypeIs
77

88
from .events import AbstractEventLoop
99

@@ -17,7 +17,7 @@ _T = TypeVar("_T")
1717
# asyncio defines 'isfuture()' in base_futures.py and re-imports it in futures.py
1818
# but it leads to circular import error in pytype tool.
1919
# That's why the import order is reversed.
20-
def isfuture(obj: object) -> TypeGuard[Future[Any]]: ...
20+
def isfuture(obj: object) -> TypeIs[Future[Any]]: ...
2121

2222
class Future(Awaitable[_T], Iterable[_T]):
2323
_state: str

mypy/typeshed/stdlib/asyncio/locks.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ class BoundedSemaphore(Semaphore): ...
101101

102102
if sys.version_info >= (3, 11):
103103
class _BarrierState(enum.Enum): # undocumented
104-
FILLING: str
105-
DRAINING: str
106-
RESETTING: str
107-
BROKEN: str
104+
FILLING = "filling"
105+
DRAINING = "draining"
106+
RESETTING = "resetting"
107+
BROKEN = "broken"
108108

109109
class Barrier(_LoopBoundMixin):
110110
def __init__(self, parties: int) -> None: ...

mypy/typeshed/stdlib/asyncio/sslproto.pyi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ if sys.version_info >= (3, 11):
1414
SSLAgainErrors: tuple[type[ssl.SSLWantReadError], type[ssl.SSLSyscallError]]
1515

1616
class SSLProtocolState(Enum):
17-
UNWRAPPED: str
18-
DO_HANDSHAKE: str
19-
WRAPPED: str
20-
FLUSHING: str
21-
SHUTDOWN: str
17+
UNWRAPPED = "UNWRAPPED"
18+
DO_HANDSHAKE = "DO_HANDSHAKE"
19+
WRAPPED = "WRAPPED"
20+
FLUSHING = "FLUSHING"
21+
SHUTDOWN = "SHUTDOWN"
2222

2323
class AppProtocolState(Enum):
24-
STATE_INIT: str
25-
STATE_CON_MADE: str
26-
STATE_EOF: str
27-
STATE_CON_LOST: str
24+
STATE_INIT = "STATE_INIT"
25+
STATE_CON_MADE = "STATE_CON_MADE"
26+
STATE_EOF = "STATE_EOF"
27+
STATE_CON_LOST = "STATE_CON_LOST"
2828

2929
def add_flowcontrol_defaults(high: int | None, low: int | None, kb: int) -> tuple[int, int]: ...
3030

mypy/typeshed/stdlib/audioop.pyi

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
from typing_extensions import TypeAlias
1+
from typing_extensions import Buffer, TypeAlias
22

33
_AdpcmState: TypeAlias = tuple[int, int]
44
_RatecvState: TypeAlias = tuple[int, tuple[tuple[int, int], ...]]
55

66
class error(Exception): ...
77

8-
def add(fragment1: bytes, fragment2: bytes, width: int, /) -> bytes: ...
9-
def adpcm2lin(fragment: bytes, width: int, state: _AdpcmState | None, /) -> tuple[bytes, _AdpcmState]: ...
10-
def alaw2lin(fragment: bytes, width: int, /) -> bytes: ...
11-
def avg(fragment: bytes, width: int, /) -> int: ...
12-
def avgpp(fragment: bytes, width: int, /) -> int: ...
13-
def bias(fragment: bytes, width: int, bias: int, /) -> bytes: ...
14-
def byteswap(fragment: bytes, width: int, /) -> bytes: ...
15-
def cross(fragment: bytes, width: int, /) -> int: ...
16-
def findfactor(fragment: bytes, reference: bytes, /) -> float: ...
17-
def findfit(fragment: bytes, reference: bytes, /) -> tuple[int, float]: ...
18-
def findmax(fragment: bytes, length: int, /) -> int: ...
19-
def getsample(fragment: bytes, width: int, index: int, /) -> int: ...
20-
def lin2adpcm(fragment: bytes, width: int, state: _AdpcmState | None, /) -> tuple[bytes, _AdpcmState]: ...
21-
def lin2alaw(fragment: bytes, width: int, /) -> bytes: ...
22-
def lin2lin(fragment: bytes, width: int, newwidth: int, /) -> bytes: ...
23-
def lin2ulaw(fragment: bytes, width: int, /) -> bytes: ...
24-
def max(fragment: bytes, width: int, /) -> int: ...
25-
def maxpp(fragment: bytes, width: int, /) -> int: ...
26-
def minmax(fragment: bytes, width: int, /) -> tuple[int, int]: ...
27-
def mul(fragment: bytes, width: int, factor: float, /) -> bytes: ...
8+
def add(fragment1: Buffer, fragment2: Buffer, width: int, /) -> bytes: ...
9+
def adpcm2lin(fragment: Buffer, width: int, state: _AdpcmState | None, /) -> tuple[bytes, _AdpcmState]: ...
10+
def alaw2lin(fragment: Buffer, width: int, /) -> bytes: ...
11+
def avg(fragment: Buffer, width: int, /) -> int: ...
12+
def avgpp(fragment: Buffer, width: int, /) -> int: ...
13+
def bias(fragment: Buffer, width: int, bias: int, /) -> bytes: ...
14+
def byteswap(fragment: Buffer, width: int, /) -> bytes: ...
15+
def cross(fragment: Buffer, width: int, /) -> int: ...
16+
def findfactor(fragment: Buffer, reference: Buffer, /) -> float: ...
17+
def findfit(fragment: Buffer, reference: Buffer, /) -> tuple[int, float]: ...
18+
def findmax(fragment: Buffer, length: int, /) -> int: ...
19+
def getsample(fragment: Buffer, width: int, index: int, /) -> int: ...
20+
def lin2adpcm(fragment: Buffer, width: int, state: _AdpcmState | None, /) -> tuple[bytes, _AdpcmState]: ...
21+
def lin2alaw(fragment: Buffer, width: int, /) -> bytes: ...
22+
def lin2lin(fragment: Buffer, width: int, newwidth: int, /) -> bytes: ...
23+
def lin2ulaw(fragment: Buffer, width: int, /) -> bytes: ...
24+
def max(fragment: Buffer, width: int, /) -> int: ...
25+
def maxpp(fragment: Buffer, width: int, /) -> int: ...
26+
def minmax(fragment: Buffer, width: int, /) -> tuple[int, int]: ...
27+
def mul(fragment: Buffer, width: int, factor: float, /) -> bytes: ...
2828
def ratecv(
29-
fragment: bytes,
29+
fragment: Buffer,
3030
width: int,
3131
nchannels: int,
3232
inrate: int,
@@ -36,8 +36,8 @@ def ratecv(
3636
weightB: int = 0,
3737
/,
3838
) -> tuple[bytes, _RatecvState]: ...
39-
def reverse(fragment: bytes, width: int, /) -> bytes: ...
40-
def rms(fragment: bytes, width: int, /) -> int: ...
41-
def tomono(fragment: bytes, width: int, lfactor: float, rfactor: float, /) -> bytes: ...
42-
def tostereo(fragment: bytes, width: int, lfactor: float, rfactor: float, /) -> bytes: ...
43-
def ulaw2lin(fragment: bytes, width: int, /) -> bytes: ...
39+
def reverse(fragment: Buffer, width: int, /) -> bytes: ...
40+
def rms(fragment: Buffer, width: int, /) -> int: ...
41+
def tomono(fragment: Buffer, width: int, lfactor: float, rfactor: float, /) -> bytes: ...
42+
def tostereo(fragment: Buffer, width: int, lfactor: float, rfactor: float, /) -> bytes: ...
43+
def ulaw2lin(fragment: Buffer, width: int, /) -> bytes: ...

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ from typing_extensions import ( # noqa: Y023
6565
Self,
6666
TypeAlias,
6767
TypeGuard,
68+
TypeIs,
6869
TypeVarTuple,
6970
deprecated,
7071
)
@@ -943,15 +944,25 @@ class dict(MutableMapping[_KT, _VT]):
943944
@overload
944945
def __init__(self) -> None: ...
945946
@overload
946-
def __init__(self: dict[str, _VT], **kwargs: _VT) -> None: ...
947+
def __init__(self: dict[str, _VT], **kwargs: _VT) -> None: ... # pyright: ignore[reportInvalidTypeVarUse] #11780
947948
@overload
948949
def __init__(self, map: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ...
949950
@overload
950-
def __init__(self: dict[str, _VT], map: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ...
951+
def __init__(
952+
self: dict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
953+
map: SupportsKeysAndGetItem[str, _VT],
954+
/,
955+
**kwargs: _VT,
956+
) -> None: ...
951957
@overload
952958
def __init__(self, iterable: Iterable[tuple[_KT, _VT]], /) -> None: ...
953959
@overload
954-
def __init__(self: dict[str, _VT], iterable: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> None: ...
960+
def __init__(
961+
self: dict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
962+
iterable: Iterable[tuple[str, _VT]],
963+
/,
964+
**kwargs: _VT,
965+
) -> None: ...
955966
# Next two overloads are for dict(string.split(sep) for string in iterable)
956967
# Cannot be Iterable[Sequence[_T]] or otherwise dict(["foo", "bar", "baz"]) is not an error
957968
@overload
@@ -1143,7 +1154,7 @@ def any(iterable: Iterable[object], /) -> bool: ...
11431154
def ascii(obj: object, /) -> str: ...
11441155
def bin(number: int | SupportsIndex, /) -> str: ...
11451156
def breakpoint(*args: Any, **kws: Any) -> None: ...
1146-
def callable(obj: object, /) -> TypeGuard[Callable[..., object]]: ...
1157+
def callable(obj: object, /) -> TypeIs[Callable[..., object]]: ...
11471158
def chr(i: int, /) -> str: ...
11481159

11491160
# We define this here instead of using os.PathLike to avoid import cycle issues.
@@ -1253,6 +1264,8 @@ class filter(Iterator[_T]):
12531264
@overload
12541265
def __new__(cls, function: Callable[[_S], TypeGuard[_T]], iterable: Iterable[_S], /) -> Self: ...
12551266
@overload
1267+
def __new__(cls, function: Callable[[_S], TypeIs[_T]], iterable: Iterable[_S], /) -> Self: ...
1268+
@overload
12561269
def __new__(cls, function: Callable[[_T], Any], iterable: Iterable[_T], /) -> Self: ...
12571270
def __iter__(self) -> Self: ...
12581271
def __next__(self) -> _T: ...

mypy/typeshed/stdlib/cgi.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import SupportsGetItem, SupportsItemAccess, Unused
1+
from _typeshed import SupportsContainsAndGetItem, SupportsGetItem, SupportsItemAccess, Unused
22
from builtins import list as _list, type as _type
33
from collections.abc import Iterable, Iterator, Mapping
44
from email.message import Message
@@ -85,7 +85,7 @@ class FieldStorage:
8585
fp: IO[Any] | None = None,
8686
headers: Mapping[str, str] | Message | None = None,
8787
outerboundary: bytes = b"",
88-
environ: SupportsGetItem[str, str] = ...,
88+
environ: SupportsContainsAndGetItem[str, str] = ...,
8989
keep_blank_values: int = 0,
9090
strict_parsing: int = 0,
9191
limit: int | None = None,

mypy/typeshed/stdlib/collections/__init__.pyi

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,27 @@ class UserDict(MutableMapping[_KT, _VT]):
5151
@overload
5252
def __init__(self, dict: None = None, /) -> None: ...
5353
@overload
54-
def __init__(self: UserDict[str, _VT], dict: None = None, /, **kwargs: _VT) -> None: ...
54+
def __init__(
55+
self: UserDict[str, _VT], dict: None = None, /, **kwargs: _VT # pyright: ignore[reportInvalidTypeVarUse] #11780
56+
) -> None: ...
5557
@overload
5658
def __init__(self, dict: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ...
5759
@overload
58-
def __init__(self: UserDict[str, _VT], dict: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ...
60+
def __init__(
61+
self: UserDict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
62+
dict: SupportsKeysAndGetItem[str, _VT],
63+
/,
64+
**kwargs: _VT,
65+
) -> None: ...
5966
@overload
6067
def __init__(self, iterable: Iterable[tuple[_KT, _VT]], /) -> None: ...
6168
@overload
62-
def __init__(self: UserDict[str, _VT], iterable: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> None: ...
69+
def __init__(
70+
self: UserDict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
71+
iterable: Iterable[tuple[str, _VT]],
72+
/,
73+
**kwargs: _VT,
74+
) -> None: ...
6375
@overload
6476
def __init__(self: UserDict[str, str], iterable: Iterable[list[str]], /) -> None: ...
6577
@overload
@@ -389,16 +401,21 @@ class defaultdict(dict[_KT, _VT]):
389401
@overload
390402
def __init__(self) -> None: ...
391403
@overload
392-
def __init__(self: defaultdict[str, _VT], **kwargs: _VT) -> None: ...
404+
def __init__(self: defaultdict[str, _VT], **kwargs: _VT) -> None: ... # pyright: ignore[reportInvalidTypeVarUse] #11780
393405
@overload
394406
def __init__(self, default_factory: Callable[[], _VT] | None, /) -> None: ...
395407
@overload
396-
def __init__(self: defaultdict[str, _VT], default_factory: Callable[[], _VT] | None, /, **kwargs: _VT) -> None: ...
408+
def __init__(
409+
self: defaultdict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
410+
default_factory: Callable[[], _VT] | None,
411+
/,
412+
**kwargs: _VT,
413+
) -> None: ...
397414
@overload
398415
def __init__(self, default_factory: Callable[[], _VT] | None, map: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ...
399416
@overload
400417
def __init__(
401-
self: defaultdict[str, _VT],
418+
self: defaultdict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
402419
default_factory: Callable[[], _VT] | None,
403420
map: SupportsKeysAndGetItem[str, _VT],
404421
/,
@@ -408,7 +425,7 @@ class defaultdict(dict[_KT, _VT]):
408425
def __init__(self, default_factory: Callable[[], _VT] | None, iterable: Iterable[tuple[_KT, _VT]], /) -> None: ...
409426
@overload
410427
def __init__(
411-
self: defaultdict[str, _VT],
428+
self: defaultdict[str, _VT], # pyright: ignore[reportInvalidTypeVarUse] #11780
412429
default_factory: Callable[[], _VT] | None,
413430
iterable: Iterable[tuple[str, _VT]],
414431
/,

0 commit comments

Comments
 (0)