Skip to content

Commit 2607dcd

Browse files
committed
Sync typeshed
Source commit: python/typeshed@616ca7d
1 parent fcabf19 commit 2607dcd

26 files changed

+303
-136
lines changed

mypy/typeshed/stdlib/_asyncio.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from asyncio.events import AbstractEventLoop
3-
from collections.abc import Awaitable, Callable, Coroutine, Generator, Iterable
3+
from collections.abc import Awaitable, Callable, Coroutine, Generator
44
from contextvars import Context
55
from types import FrameType
66
from typing import Any, Literal, TextIO, TypeVar
@@ -13,7 +13,7 @@ _T = TypeVar("_T")
1313
_T_co = TypeVar("_T_co", covariant=True)
1414
_TaskYieldType: TypeAlias = Future[object] | None
1515

16-
class Future(Awaitable[_T], Iterable[_T]):
16+
class Future(Awaitable[_T]):
1717
_state: str
1818
@property
1919
def _exception(self) -> BaseException | None: ...

mypy/typeshed/stdlib/_ctypes.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ class Array(_CData, Generic[_CT], metaclass=_PyCArrayType):
286286
def _type_(self) -> type[_CT]: ...
287287
@_type_.setter
288288
def _type_(self, value: type[_CT]) -> None: ...
289-
raw: bytes # Note: only available if _CT == c_char
289+
# Note: only available if _CT == c_char
290+
@property
291+
def raw(self) -> bytes: ...
292+
@raw.setter
293+
def raw(self, value: ReadableBuffer) -> None: ...
290294
value: Any # Note: bytes if _CT == c_char, str if _CT == c_wchar, unavailable otherwise
291295
# TODO These methods cannot be annotated correctly at the moment.
292296
# All of these "Any"s stand for the array's element type, but it's not possible to use _CT

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# See the README.md file in this directory for more information.
44

55
import sys
6+
import typing_extensions
67
from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized
78
from dataclasses import Field
89
from os import PathLike
@@ -328,9 +329,9 @@ class structseq(Generic[_T_co]):
328329
# The second parameter will accept a dict of any kind without raising an exception,
329330
# but only has any meaning if you supply it a dict where the keys are strings.
330331
# https://github.com/python/typeshed/pull/6560#discussion_r767149830
331-
def __new__(cls: type[Self], sequence: Iterable[_T_co], dict: dict[str, Any] = ...) -> Self: ...
332+
def __new__(cls, sequence: Iterable[_T_co], dict: dict[str, Any] = ...) -> typing_extensions.Self: ...
332333
if sys.version_info >= (3, 13):
333-
def __replace__(self: Self, **kwargs: Any) -> Self: ...
334+
def __replace__(self, **kwargs: Any) -> typing_extensions.Self: ...
334335

335336
# Superset of typing.AnyStr that also includes LiteralString
336337
AnyOrLiteralStr = TypeVar("AnyOrLiteralStr", str, bytes, LiteralString) # noqa: Y001

mypy/typeshed/stdlib/argparse.pyi

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import SupportsWrite, sentinel
33
from collections.abc import Callable, Generator, Iterable, Sequence
44
from re import Pattern
5-
from typing import IO, Any, ClassVar, Final, Generic, NewType, NoReturn, Protocol, TypeVar, overload
5+
from typing import IO, Any, ClassVar, Final, Generic, NoReturn, Protocol, TypeVar, overload
66
from typing_extensions import Self, TypeAlias, deprecated
77

88
__all__ = [
@@ -38,9 +38,7 @@ ONE_OR_MORE: Final = "+"
3838
OPTIONAL: Final = "?"
3939
PARSER: Final = "A..."
4040
REMAINDER: Final = "..."
41-
_SUPPRESS_T = NewType("_SUPPRESS_T", str)
42-
SUPPRESS: _SUPPRESS_T | str # not using Literal because argparse sometimes compares SUPPRESS with is
43-
# the | str is there so that foo = argparse.SUPPRESS; foo = "test" checks out in mypy
41+
SUPPRESS: Final = "==SUPPRESS=="
4442
ZERO_OR_MORE: Final = "*"
4543
_UNRECOGNIZED_ARGS_ATTR: Final = "_unrecognized_args" # undocumented
4644

@@ -83,7 +81,7 @@ class _ActionsContainer:
8381
# more precisely, Literal["?", "*", "+", "...", "A...", "==SUPPRESS=="],
8482
# but using this would make it hard to annotate callers that don't use a
8583
# literal argument and for subclasses to override this method.
86-
nargs: int | str | _SUPPRESS_T | None = None,
84+
nargs: int | str | None = None,
8785
const: Any = ...,
8886
default: Any = ...,
8987
type: _ActionType = ...,

mypy/typeshed/stdlib/asyncio/sslproto.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class _SSLProtocolTransport(transports._FlowControlMixin, transports.Transport):
7676
def get_extra_info(self, name: str, default: Any | None = None) -> dict[str, Any]: ...
7777
@property
7878
def _protocol_paused(self) -> bool: ...
79-
def write(self, data: bytes | bytearray | memoryview) -> None: ...
79+
def write(self, data: bytes | bytearray | memoryview[Any]) -> None: ... # any memoryview format or shape
8080
def can_write_eof(self) -> Literal[False]: ...
8181
if sys.version_info >= (3, 11):
8282
def get_write_buffer_limits(self) -> tuple[int, int]: ...

mypy/typeshed/stdlib/asyncio/transports.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ class WriteTransport(BaseTransport):
2424
def set_write_buffer_limits(self, high: int | None = None, low: int | None = None) -> None: ...
2525
def get_write_buffer_size(self) -> int: ...
2626
def get_write_buffer_limits(self) -> tuple[int, int]: ...
27-
def write(self, data: bytes | bytearray | memoryview) -> None: ...
28-
def writelines(self, list_of_data: Iterable[bytes | bytearray | memoryview]) -> None: ...
27+
def write(self, data: bytes | bytearray | memoryview[Any]) -> None: ... # any memoryview format or shape
28+
def writelines(
29+
self, list_of_data: Iterable[bytes | bytearray | memoryview[Any]]
30+
) -> None: ... # any memoryview format or shape
2931
def write_eof(self) -> None: ...
3032
def can_write_eof(self) -> bool: ...
3133
def abort(self) -> None: ...

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 105 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ from typing import ( # noqa: Y022
6262
from typing_extensions import ( # noqa: Y023
6363
Concatenate,
6464
Literal,
65+
LiteralString,
6566
ParamSpec,
6667
Self,
6768
TypeAlias,
@@ -444,16 +445,31 @@ class str(Sequence[str]):
444445
def __new__(cls, object: object = ...) -> Self: ...
445446
@overload
446447
def __new__(cls, object: ReadableBuffer, encoding: str = ..., errors: str = ...) -> Self: ...
448+
@overload
449+
def capitalize(self: LiteralString) -> LiteralString: ...
450+
@overload
447451
def capitalize(self) -> str: ... # type: ignore[misc]
452+
@overload
453+
def casefold(self: LiteralString) -> LiteralString: ...
454+
@overload
448455
def casefold(self) -> str: ... # type: ignore[misc]
456+
@overload
457+
def center(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ...
458+
@overload
449459
def center(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]
450460
def count(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
451461
def encode(self, encoding: str = "utf-8", errors: str = "strict") -> bytes: ...
452462
def endswith(
453463
self, suffix: str | tuple[str, ...], start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /
454464
) -> bool: ...
465+
@overload
466+
def expandtabs(self: LiteralString, tabsize: SupportsIndex = 8) -> LiteralString: ...
467+
@overload
455468
def expandtabs(self, tabsize: SupportsIndex = 8) -> str: ... # type: ignore[misc]
456469
def find(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
470+
@overload
471+
def format(self: LiteralString, *args: LiteralString, **kwargs: LiteralString) -> LiteralString: ...
472+
@overload
457473
def format(self, *args: object, **kwargs: object) -> str: ...
458474
def format_map(self, mapping: _FormatMapMapping, /) -> str: ...
459475
def index(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
@@ -469,35 +485,99 @@ class str(Sequence[str]):
469485
def isspace(self) -> bool: ...
470486
def istitle(self) -> bool: ...
471487
def isupper(self) -> bool: ...
488+
@overload
489+
def join(self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString: ...
490+
@overload
472491
def join(self, iterable: Iterable[str], /) -> str: ... # type: ignore[misc]
492+
@overload
493+
def ljust(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ...
494+
@overload
473495
def ljust(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]
496+
@overload
497+
def lower(self: LiteralString) -> LiteralString: ...
498+
@overload
474499
def lower(self) -> str: ... # type: ignore[misc]
500+
@overload
501+
def lstrip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ...
502+
@overload
475503
def lstrip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc]
504+
@overload
505+
def partition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ...
506+
@overload
476507
def partition(self, sep: str, /) -> tuple[str, str, str]: ... # type: ignore[misc]
477508
if sys.version_info >= (3, 13):
509+
@overload
510+
def replace(
511+
self: LiteralString, old: LiteralString, new: LiteralString, /, count: SupportsIndex = -1
512+
) -> LiteralString: ...
513+
@overload
478514
def replace(self, old: str, new: str, /, count: SupportsIndex = -1) -> str: ... # type: ignore[misc]
479515
else:
516+
@overload
517+
def replace(
518+
self: LiteralString, old: LiteralString, new: LiteralString, count: SupportsIndex = -1, /
519+
) -> LiteralString: ...
520+
@overload
480521
def replace(self, old: str, new: str, count: SupportsIndex = -1, /) -> str: ... # type: ignore[misc]
481522
if sys.version_info >= (3, 9):
523+
@overload
524+
def removeprefix(self: LiteralString, prefix: LiteralString, /) -> LiteralString: ...
525+
@overload
482526
def removeprefix(self, prefix: str, /) -> str: ... # type: ignore[misc]
527+
@overload
528+
def removesuffix(self: LiteralString, suffix: LiteralString, /) -> LiteralString: ...
529+
@overload
483530
def removesuffix(self, suffix: str, /) -> str: ... # type: ignore[misc]
484531

485532
def rfind(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
486533
def rindex(self, sub: str, start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /) -> int: ...
534+
@overload
535+
def rjust(self: LiteralString, width: SupportsIndex, fillchar: LiteralString = " ", /) -> LiteralString: ...
536+
@overload
487537
def rjust(self, width: SupportsIndex, fillchar: str = " ", /) -> str: ... # type: ignore[misc]
538+
@overload
539+
def rpartition(self: LiteralString, sep: LiteralString, /) -> tuple[LiteralString, LiteralString, LiteralString]: ...
540+
@overload
488541
def rpartition(self, sep: str, /) -> tuple[str, str, str]: ... # type: ignore[misc]
542+
@overload
543+
def rsplit(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
544+
@overload
489545
def rsplit(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
546+
@overload
547+
def rstrip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ...
548+
@overload
490549
def rstrip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc]
550+
@overload
551+
def split(self: LiteralString, sep: LiteralString | None = None, maxsplit: SupportsIndex = -1) -> list[LiteralString]: ...
552+
@overload
491553
def split(self, sep: str | None = None, maxsplit: SupportsIndex = -1) -> list[str]: ... # type: ignore[misc]
554+
@overload
555+
def splitlines(self: LiteralString, keepends: bool = False) -> list[LiteralString]: ...
556+
@overload
492557
def splitlines(self, keepends: bool = False) -> list[str]: ... # type: ignore[misc]
493558
def startswith(
494559
self, prefix: str | tuple[str, ...], start: SupportsIndex | None = ..., end: SupportsIndex | None = ..., /
495560
) -> bool: ...
561+
@overload
562+
def strip(self: LiteralString, chars: LiteralString | None = None, /) -> LiteralString: ...
563+
@overload
496564
def strip(self, chars: str | None = None, /) -> str: ... # type: ignore[misc]
565+
@overload
566+
def swapcase(self: LiteralString) -> LiteralString: ...
567+
@overload
497568
def swapcase(self) -> str: ... # type: ignore[misc]
569+
@overload
570+
def title(self: LiteralString) -> LiteralString: ...
571+
@overload
498572
def title(self) -> str: ... # type: ignore[misc]
499573
def translate(self, table: _TranslateTable, /) -> str: ...
574+
@overload
575+
def upper(self: LiteralString) -> LiteralString: ...
576+
@overload
500577
def upper(self) -> str: ... # type: ignore[misc]
578+
@overload
579+
def zfill(self: LiteralString, width: SupportsIndex, /) -> LiteralString: ...
580+
@overload
501581
def zfill(self, width: SupportsIndex, /) -> str: ... # type: ignore[misc]
502582
@staticmethod
503583
@overload
@@ -508,21 +588,39 @@ class str(Sequence[str]):
508588
@staticmethod
509589
@overload
510590
def maketrans(x: str, y: str, z: str, /) -> dict[int, int | None]: ...
591+
@overload
592+
def __add__(self: LiteralString, value: LiteralString, /) -> LiteralString: ...
593+
@overload
511594
def __add__(self, value: str, /) -> str: ... # type: ignore[misc]
512595
# Incompatible with Sequence.__contains__
513596
def __contains__(self, key: str, /) -> bool: ... # type: ignore[override]
514597
def __eq__(self, value: object, /) -> bool: ...
515598
def __ge__(self, value: str, /) -> bool: ...
516-
def __getitem__(self, key: SupportsIndex | slice, /) -> str: ...
599+
@overload
600+
def __getitem__(self: LiteralString, key: SupportsIndex | slice, /) -> LiteralString: ...
601+
@overload
602+
def __getitem__(self, key: SupportsIndex | slice, /) -> str: ... # type: ignore[misc]
517603
def __gt__(self, value: str, /) -> bool: ...
518604
def __hash__(self) -> int: ...
605+
@overload
606+
def __iter__(self: LiteralString) -> Iterator[LiteralString]: ...
607+
@overload
519608
def __iter__(self) -> Iterator[str]: ... # type: ignore[misc]
520609
def __le__(self, value: str, /) -> bool: ...
521610
def __len__(self) -> int: ...
522611
def __lt__(self, value: str, /) -> bool: ...
612+
@overload
613+
def __mod__(self: LiteralString, value: LiteralString | tuple[LiteralString, ...], /) -> LiteralString: ...
614+
@overload
523615
def __mod__(self, value: Any, /) -> str: ...
616+
@overload
617+
def __mul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ...
618+
@overload
524619
def __mul__(self, value: SupportsIndex, /) -> str: ... # type: ignore[misc]
525620
def __ne__(self, value: object, /) -> bool: ...
621+
@overload
622+
def __rmul__(self: LiteralString, value: SupportsIndex, /) -> LiteralString: ...
623+
@overload
526624
def __rmul__(self, value: SupportsIndex, /) -> str: ... # type: ignore[misc]
527625
def __getnewargs__(self) -> tuple[str]: ...
528626

@@ -1149,7 +1247,7 @@ class frozenset(AbstractSet[_T_co]):
11491247
if sys.version_info >= (3, 9):
11501248
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
11511249

1152-
class enumerate(Iterator[tuple[int, _T]]):
1250+
class enumerate(Generic[_T]):
11531251
def __new__(cls, iterable: Iterable[_T], start: int = 0) -> Self: ...
11541252
def __iter__(self) -> Self: ...
11551253
def __next__(self) -> tuple[int, _T]: ...
@@ -1343,7 +1441,7 @@ else:
13431441

13441442
exit: _sitebuiltins.Quitter
13451443

1346-
class filter(Iterator[_T]):
1444+
class filter(Generic[_T]):
13471445
@overload
13481446
def __new__(cls, function: None, iterable: Iterable[_T | None], /) -> Self: ...
13491447
@overload
@@ -1408,7 +1506,7 @@ license: _sitebuiltins._Printer
14081506

14091507
def locals() -> dict[str, Any]: ...
14101508

1411-
class map(Iterator[_S]):
1509+
class map(Generic[_S]):
14121510
@overload
14131511
def __new__(cls, func: Callable[[_T1], _S], iterable: Iterable[_T1], /) -> Self: ...
14141512
@overload
@@ -1651,7 +1749,7 @@ def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex
16511749

16521750
quit: _sitebuiltins.Quitter
16531751

1654-
class reversed(Iterator[_T]):
1752+
class reversed(Generic[_T]):
16551753
@overload
16561754
def __new__(cls, sequence: Reversible[_T], /) -> Iterator[_T]: ... # type: ignore[misc]
16571755
@overload
@@ -1699,7 +1797,7 @@ _SupportsSumNoDefaultT = TypeVar("_SupportsSumNoDefaultT", bound=_SupportsSumWit
16991797
# without creating many false-positive errors (see #7578).
17001798
# Instead, we special-case the most common examples of this: bool and literal integers.
17011799
@overload
1702-
def sum(iterable: Iterable[bool], /, start: int = 0) -> int: ...
1800+
def sum(iterable: Iterable[bool | _LiteralInteger], /, start: int = 0) -> int: ...
17031801
@overload
17041802
def sum(iterable: Iterable[_SupportsSumNoDefaultT], /) -> _SupportsSumNoDefaultT | Literal[0]: ...
17051803
@overload
@@ -1712,7 +1810,7 @@ def vars(object: type, /) -> types.MappingProxyType[str, Any]: ...
17121810
@overload
17131811
def vars(object: Any = ..., /) -> dict[str, Any]: ...
17141812

1715-
class zip(Iterator[_T_co]):
1813+
class zip(Generic[_T_co]):
17161814
if sys.version_info >= (3, 10):
17171815
@overload
17181816
def __new__(cls, *, strict: bool = ...) -> zip[Any]: ...

mypy/typeshed/stdlib/csv.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ else:
2525
from _csv import _reader as Reader, _writer as Writer
2626

2727
from _typeshed import SupportsWrite
28-
from collections.abc import Collection, Iterable, Iterator, Mapping, Sequence
28+
from collections.abc import Collection, Iterable, Mapping, Sequence
2929
from typing import Any, Generic, Literal, TypeVar, overload
3030
from typing_extensions import Self
3131

@@ -75,7 +75,7 @@ class excel(Dialect): ...
7575
class excel_tab(excel): ...
7676
class unix_dialect(Dialect): ...
7777

78-
class DictReader(Iterator[dict[_T | Any, str | Any]], Generic[_T]):
78+
class DictReader(Generic[_T]):
7979
fieldnames: Sequence[_T] | None
8080
restkey: _T | None
8181
restval: str | Any | None

mypy/typeshed/stdlib/fileinput.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import sys
22
from _typeshed import AnyStr_co, StrOrBytesPath
3-
from collections.abc import Callable, Iterable, Iterator
3+
from collections.abc import Callable, Iterable
44
from types import TracebackType
5-
from typing import IO, Any, AnyStr, Literal, Protocol, overload
5+
from typing import IO, Any, AnyStr, Generic, Literal, Protocol, overload
66
from typing_extensions import Self, TypeAlias
77

88
if sys.version_info >= (3, 9):
@@ -107,7 +107,7 @@ def fileno() -> int: ...
107107
def isfirstline() -> bool: ...
108108
def isstdin() -> bool: ...
109109

110-
class FileInput(Iterator[AnyStr]):
110+
class FileInput(Generic[AnyStr]):
111111
if sys.version_info >= (3, 10):
112112
# encoding and errors are added
113113
@overload

mypy/typeshed/stdlib/heapq.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from _heapq import *
22
from _typeshed import SupportsRichComparison
3-
from collections.abc import Callable, Iterable
3+
from collections.abc import Callable, Generator, Iterable
44
from typing import Any, Final, TypeVar
55

66
__all__ = ["heappush", "heappop", "heapify", "heapreplace", "merge", "nlargest", "nsmallest", "heappushpop"]
@@ -11,7 +11,7 @@ __about__: Final[str]
1111

1212
def merge(
1313
*iterables: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None, reverse: bool = False
14-
) -> Iterable[_S]: ...
14+
) -> Generator[_S]: ...
1515
def nlargest(n: int, iterable: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None) -> list[_S]: ...
1616
def nsmallest(n: int, iterable: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None) -> list[_S]: ...
1717
def _heapify_max(heap: list[Any], /) -> None: ... # undocumented

0 commit comments

Comments
 (0)