diff --git a/mypy/typeshed/stdlib/VERSIONS b/mypy/typeshed/stdlib/VERSIONS index d24aa35faf6e..86e8da78677c 100644 --- a/mypy/typeshed/stdlib/VERSIONS +++ b/mypy/typeshed/stdlib/VERSIONS @@ -61,7 +61,7 @@ antigravity: 2.7- argparse: 2.7- array: 2.7- ast: 2.7- -asynchat: 2.7- +asynchat: 2.7-3.11 asyncio: 3.4- asyncio.mixins: 3.10- asyncio.exceptions: 3.8- @@ -72,7 +72,7 @@ asyncio.taskgroups: 3.11- asyncio.threads: 3.9- asyncio.timeouts: 3.11- asyncio.trsock: 3.8- -asyncore: 2.7- +asyncore: 2.7-3.11 atexit: 2.7- audioop: 2.7- base64: 2.7- @@ -228,7 +228,7 @@ shlex: 2.7- shutil: 2.7- signal: 2.7- site: 2.7- -smtpd: 2.7- +smtpd: 2.7-3.11 smtplib: 2.7- sndhdr: 2.7- socket: 2.7- diff --git a/mypy/typeshed/stdlib/_collections_abc.pyi b/mypy/typeshed/stdlib/_collections_abc.pyi index 352da6cfb331..05b5421c21f3 100644 --- a/mypy/typeshed/stdlib/_collections_abc.pyi +++ b/mypy/typeshed/stdlib/_collections_abc.pyi @@ -1,4 +1,5 @@ import sys +from abc import abstractmethod from types import MappingProxyType from typing import ( # noqa: Y022,Y038 AbstractSet as Set, @@ -23,11 +24,13 @@ from typing import ( # noqa: Y022,Y038 MutableMapping as MutableMapping, MutableSequence as MutableSequence, MutableSet as MutableSet, + Protocol, Reversible as Reversible, Sequence as Sequence, Sized as Sized, TypeVar, ValuesView as ValuesView, + runtime_checkable, ) from typing_extensions import final @@ -58,6 +61,8 @@ __all__ = [ "MutableSequence", "ByteString", ] +if sys.version_info >= (3, 12): + __all__ += ["Buffer"] _KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers. _VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers. @@ -79,3 +84,9 @@ class dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): # undocum if sys.version_info >= (3, 10): @property def mapping(self) -> MappingProxyType[_KT_co, _VT_co]: ... + +if sys.version_info >= (3, 12): + @runtime_checkable + class Buffer(Protocol): + @abstractmethod + def __buffer__(self, __flags: int) -> memoryview: ... diff --git a/mypy/typeshed/stdlib/_ctypes.pyi b/mypy/typeshed/stdlib/_ctypes.pyi index 4f8de4605d4d..8e8bcbe84bd4 100644 --- a/mypy/typeshed/stdlib/_ctypes.pyi +++ b/mypy/typeshed/stdlib/_ctypes.pyi @@ -63,6 +63,8 @@ class _CData(metaclass=_CDataMeta): def from_param(cls, obj: Any) -> Self | _CArgObject: ... @classmethod def in_dll(cls, library: CDLL, name: str) -> Self: ... + def __buffer__(self, __flags: int) -> memoryview: ... + def __release_buffer__(self, __buffer: memoryview) -> None: ... class _SimpleCData(Generic[_T], _CData): value: _T diff --git a/mypy/typeshed/stdlib/_typeshed/__init__.pyi b/mypy/typeshed/stdlib/_typeshed/__init__.pyi index 3c29032b6b0d..5d03142c6d71 100644 --- a/mypy/typeshed/stdlib/_typeshed/__init__.pyi +++ b/mypy/typeshed/stdlib/_typeshed/__init__.pyi @@ -2,17 +2,13 @@ # # See the README.md file in this directory for more information. -import array -import ctypes -import mmap -import pickle import sys -from collections.abc import Awaitable, Callable, Iterable, Set as AbstractSet +from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized from dataclasses import Field from os import PathLike from types import FrameType, TracebackType -from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar -from typing_extensions import Final, Literal, LiteralString, TypeAlias, final +from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar, overload +from typing_extensions import Buffer, Final, Literal, LiteralString, TypeAlias, final _KT = TypeVar("_KT") _KT_co = TypeVar("_KT_co", covariant=True) @@ -227,42 +223,33 @@ class SupportsNoArgReadline(Protocol[_T_co]): class SupportsWrite(Protocol[_T_contra]): def write(self, __s: _T_contra) -> object: ... -ReadOnlyBuffer: TypeAlias = bytes # stable +# Unfortunately PEP 688 does not allow us to distinguish read-only +# from writable buffers. We use these aliases for readability for now. +# Perhaps a future extension of the buffer protocol will allow us to +# distinguish these cases in the type system. +ReadOnlyBuffer: TypeAlias = Buffer # stable # Anything that implements the read-write buffer interface. -# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol -# for it (until PEP 688 is implemented). Instead we have to list the most common stdlib buffer classes in a Union. -if sys.version_info >= (3, 8): - WriteableBuffer: TypeAlias = ( - bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData | pickle.PickleBuffer - ) # stable -else: - WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable -# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes). -ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable -_BufferWithLen: TypeAlias = ReadableBuffer # not stable # noqa: Y047 - -# Anything that implements the read-write buffer interface, and can be sliced/indexed. -SliceableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] | mmap.mmap -IndexableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] | mmap.mmap -# https://github.com/python/typeshed/pull/9115#issuecomment-1304905864 -# Post PEP 688, they should be rewritten as such: -# from collections.abc import Sequence -# from typing import Sized, overload -# class SliceableBuffer(Protocol): -# def __buffer__(self, __flags: int) -> memoryview: ... -# def __getitem__(self, __slice: slice) -> Sequence[int]: ... -# class IndexableBuffer(Protocol): -# def __buffer__(self, __flags: int) -> memoryview: ... -# def __getitem__(self, __i: int) -> int: ... -# class SupportsGetItemBuffer(SliceableBuffer, IndexableBuffer, Protocol): -# def __buffer__(self, __flags: int) -> memoryview: ... -# def __contains__(self, __x: Any) -> bool: ... -# @overload -# def __getitem__(self, __slice: slice) -> Sequence[int]: ... -# @overload -# def __getitem__(self, __i: int) -> int: ... -# class SizedBuffer(Sized, Protocol): # instead of _BufferWithLen -# def __buffer__(self, __flags: int) -> memoryview: ... +WriteableBuffer: TypeAlias = Buffer +# Same as WriteableBuffer, but also includes read-only buffer types (like bytes). +ReadableBuffer: TypeAlias = Buffer # stable + +class SliceableBuffer(Buffer, Protocol): + def __getitem__(self, __slice: slice) -> Sequence[int]: ... + +class IndexableBuffer(Buffer, Protocol): + def __getitem__(self, __i: int) -> int: ... + +class SupportsGetItemBuffer(SliceableBuffer, IndexableBuffer, Protocol): + def __contains__(self, __x: Any) -> bool: ... + @overload + def __getitem__(self, __slice: slice) -> Sequence[int]: ... + @overload + def __getitem__(self, __i: int) -> int: ... + +class SizedBuffer(Sized, Buffer, Protocol): ... + +# for compatibility with third-party stubs that may use this +_BufferWithLen: TypeAlias = SizedBuffer # not stable # noqa: Y047 ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType] OptExcInfo: TypeAlias = ExcInfo | tuple[None, None, None] diff --git a/mypy/typeshed/stdlib/argparse.pyi b/mypy/typeshed/stdlib/argparse.pyi index 1e956069314b..c986b9cdb082 100644 --- a/mypy/typeshed/stdlib/argparse.pyi +++ b/mypy/typeshed/stdlib/argparse.pyi @@ -2,7 +2,7 @@ import sys from collections.abc import Callable, Generator, Iterable, Sequence from re import Pattern from typing import IO, Any, Generic, NewType, NoReturn, Protocol, TypeVar, overload -from typing_extensions import Literal, TypeAlias +from typing_extensions import Literal, Self, TypeAlias __all__ = [ "ArgumentParser", @@ -236,11 +236,19 @@ class HelpFormatter: _current_indent: int _level: int _action_max_length: int - _root_section: Any - _current_section: Any + _root_section: _Section + _current_section: _Section _whitespace_matcher: Pattern[str] _long_break_matcher: Pattern[str] - _Section: type[Any] # Nested class + + class _Section: + formatter: HelpFormatter + heading: str | None + parent: Self | None + items: list[tuple[Callable[..., str], Iterable[Any]]] + def __init__(self, formatter: HelpFormatter, parent: Self | None, heading: str | None = None) -> None: ... + def format_help(self) -> str: ... + def __init__(self, prog: str, indent_increment: int = 2, max_help_position: int = 24, width: int | None = None) -> None: ... def _indent(self) -> None: ... def _dedent(self) -> None: ... @@ -249,16 +257,16 @@ class HelpFormatter: def end_section(self) -> None: ... def add_text(self, text: str | None) -> None: ... def add_usage( - self, usage: str | None, actions: Iterable[Action], groups: Iterable[_ArgumentGroup], prefix: str | None = None + self, usage: str | None, actions: Iterable[Action], groups: Iterable[_MutuallyExclusiveGroup], prefix: str | None = None ) -> None: ... def add_argument(self, action: Action) -> None: ... def add_arguments(self, actions: Iterable[Action]) -> None: ... def format_help(self) -> str: ... def _join_parts(self, part_strings: Iterable[str]) -> str: ... def _format_usage( - self, usage: str | None, actions: Iterable[Action], groups: Iterable[_ArgumentGroup], prefix: str | None + self, usage: str | None, actions: Iterable[Action], groups: Iterable[_MutuallyExclusiveGroup], prefix: str | None ) -> str: ... - def _format_actions_usage(self, actions: Iterable[Action], groups: Iterable[_ArgumentGroup]) -> str: ... + def _format_actions_usage(self, actions: Iterable[Action], groups: Iterable[_MutuallyExclusiveGroup]) -> str: ... def _format_text(self, text: str) -> str: ... def _format_action(self, action: Action) -> str: ... def _format_action_invocation(self, action: Action) -> str: ... diff --git a/mypy/typeshed/stdlib/array.pyi b/mypy/typeshed/stdlib/array.pyi index 38a815b584cd..8b003503bc9b 100644 --- a/mypy/typeshed/stdlib/array.pyi +++ b/mypy/typeshed/stdlib/array.pyi @@ -80,5 +80,7 @@ class array(MutableSequence[_T], Generic[_T]): def __rmul__(self, __value: int) -> array[_T]: ... def __copy__(self) -> array[_T]: ... def __deepcopy__(self, __unused: Any) -> array[_T]: ... + def __buffer__(self, __flags: int) -> memoryview: ... + def __release_buffer__(self, __buffer: memoryview) -> None: ... ArrayType = array diff --git a/mypy/typeshed/stdlib/asyncio/__init__.pyi b/mypy/typeshed/stdlib/asyncio/__init__.pyi index c4e000d2323a..2ce066cac982 100644 --- a/mypy/typeshed/stdlib/asyncio/__init__.pyi +++ b/mypy/typeshed/stdlib/asyncio/__init__.pyi @@ -1,5 +1,5 @@ import sys -from collections.abc import Coroutine, Generator +from collections.abc import Awaitable, Coroutine, Generator from typing import Any, TypeVar from typing_extensions import TypeAlias @@ -36,6 +36,8 @@ _T = TypeVar("_T") # Aliases imported by multiple submodules in typeshed if sys.version_info >= (3, 12): + _AwaitableLike: TypeAlias = Awaitable[_T] _CoroutineLike: TypeAlias = Coroutine[Any, Any, _T] else: + _AwaitableLike: TypeAlias = Generator[Any, None, _T] | Awaitable[_T] _CoroutineLike: TypeAlias = Generator[Any, None, _T] | Coroutine[Any, Any, _T] diff --git a/mypy/typeshed/stdlib/asyncio/base_events.pyi b/mypy/typeshed/stdlib/asyncio/base_events.pyi index 9dcefc8b3308..fd765fdb0614 100644 --- a/mypy/typeshed/stdlib/asyncio/base_events.pyi +++ b/mypy/typeshed/stdlib/asyncio/base_events.pyi @@ -1,13 +1,13 @@ import ssl import sys from _typeshed import FileDescriptorLike, ReadableBuffer, WriteableBuffer -from asyncio import _CoroutineLike +from asyncio import _AwaitableLike, _CoroutineLike from asyncio.events import AbstractEventLoop, AbstractServer, Handle, TimerHandle, _TaskFactory from asyncio.futures import Future from asyncio.protocols import BaseProtocol from asyncio.tasks import Task from asyncio.transports import BaseTransport, DatagramTransport, ReadTransport, SubprocessTransport, Transport, WriteTransport -from collections.abc import Awaitable, Callable, Generator, Iterable, Sequence +from collections.abc import Callable, Iterable, Sequence from contextvars import Context from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket from typing import IO, Any, TypeVar, overload @@ -64,11 +64,7 @@ class Server(AbstractServer): class BaseEventLoop(AbstractEventLoop): def run_forever(self) -> None: ... - # Can't use a union, see mypy issue # 1873. - @overload - def run_until_complete(self, future: Generator[Any, None, _T]) -> _T: ... - @overload - def run_until_complete(self, future: Awaitable[_T]) -> _T: ... + def run_until_complete(self, future: _AwaitableLike[_T]) -> _T: ... def stop(self) -> None: ... def is_running(self) -> bool: ... def is_closed(self) -> bool: ... diff --git a/mypy/typeshed/stdlib/asyncio/events.pyi b/mypy/typeshed/stdlib/asyncio/events.pyi index 4776aaa5df0c..11112bb2e87d 100644 --- a/mypy/typeshed/stdlib/asyncio/events.pyi +++ b/mypy/typeshed/stdlib/asyncio/events.pyi @@ -2,13 +2,13 @@ import ssl import sys from _typeshed import FileDescriptorLike, ReadableBuffer, StrPath, Unused, WriteableBuffer from abc import ABCMeta, abstractmethod -from collections.abc import Awaitable, Callable, Coroutine, Generator, Sequence +from collections.abc import Callable, Coroutine, Generator, Sequence from contextvars import Context from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket from typing import IO, Any, Protocol, TypeVar, overload from typing_extensions import Literal, Self, TypeAlias -from . import _CoroutineLike +from . import _AwaitableLike, _CoroutineLike from .base_events import Server from .futures import Future from .protocols import BaseProtocol @@ -113,13 +113,8 @@ class AbstractEventLoop: slow_callback_duration: float @abstractmethod def run_forever(self) -> None: ... - # Can't use a union, see mypy issue # 1873. - @overload @abstractmethod - def run_until_complete(self, future: Generator[Any, None, _T]) -> _T: ... - @overload - @abstractmethod - def run_until_complete(self, future: Awaitable[_T]) -> _T: ... + def run_until_complete(self, future: _AwaitableLike[_T]) -> _T: ... @abstractmethod def stop(self) -> None: ... @abstractmethod diff --git a/mypy/typeshed/stdlib/asyncio/tasks.pyi b/mypy/typeshed/stdlib/asyncio/tasks.pyi index 478f19055465..d8c101f281fc 100644 --- a/mypy/typeshed/stdlib/asyncio/tasks.pyi +++ b/mypy/typeshed/stdlib/asyncio/tasks.pyi @@ -1,6 +1,6 @@ import concurrent.futures import sys -from collections.abc import Awaitable, Generator, Iterable, Iterator +from collections.abc import Awaitable, Coroutine, Generator, Iterable, Iterator from types import FrameType from typing import Any, Generic, TextIO, TypeVar, overload from typing_extensions import Literal, TypeAlias @@ -275,6 +275,11 @@ else: ) -> tuple[set[Task[_T]], set[Task[_T]]]: ... async def wait_for(fut: _FutureLike[_T], timeout: float | None, *, loop: AbstractEventLoop | None = None) -> _T: ... +if sys.version_info >= (3, 12): + _TaskCompatibleCoro: TypeAlias = Coroutine[Any, Any, _T_co] +else: + _TaskCompatibleCoro: TypeAlias = Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co] + # mypy and pyright complain that a subclass of an invariant class shouldn't be covariant. # While this is true in general, here it's sort-of okay to have a covariant subclass, # since the only reason why `asyncio.Future` is invariant is the `set_result()` method, @@ -282,18 +287,12 @@ else: class Task(Future[_T_co], Generic[_T_co]): # type: ignore[type-var] # pyright: ignore[reportGeneralTypeIssues] if sys.version_info >= (3, 8): def __init__( - self, - coro: Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co], - *, - loop: AbstractEventLoop = ..., - name: str | None = ..., + self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop = ..., name: str | None = ... ) -> None: ... else: - def __init__( - self, coro: Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co], *, loop: AbstractEventLoop = ... - ) -> None: ... + def __init__(self, coro: _TaskCompatibleCoro[_T_co], *, loop: AbstractEventLoop = ...) -> None: ... if sys.version_info >= (3, 8): - def get_coro(self) -> Generator[_TaskYieldType, None, _T_co] | Awaitable[_T_co]: ... + def get_coro(self) -> _TaskCompatibleCoro[_T_co]: ... def get_name(self) -> str: ... def set_name(self, __value: object) -> None: ... diff --git a/mypy/typeshed/stdlib/binhex.pyi b/mypy/typeshed/stdlib/binhex.pyi index e0993c840ce7..64ba9f6150b4 100644 --- a/mypy/typeshed/stdlib/binhex.pyi +++ b/mypy/typeshed/stdlib/binhex.pyi @@ -1,4 +1,4 @@ -from _typeshed import _BufferWithLen +from _typeshed import SizedBuffer from typing import IO, Any from typing_extensions import Literal, TypeAlias @@ -28,9 +28,9 @@ class openrsrc: class BinHex: def __init__(self, name_finfo_dlen_rlen: _FileInfoTuple, ofp: _FileHandleUnion) -> None: ... - def write(self, data: _BufferWithLen) -> None: ... + def write(self, data: SizedBuffer) -> None: ... def close_data(self) -> None: ... - def write_rsrc(self, data: _BufferWithLen) -> None: ... + def write_rsrc(self, data: SizedBuffer) -> None: ... def close(self) -> None: ... def binhex(inp: str, out: str) -> None: ... diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi index c72da4aba335..0676aba1277e 100644 --- a/mypy/typeshed/stdlib/builtins.pyi +++ b/mypy/typeshed/stdlib/builtins.pyi @@ -36,7 +36,6 @@ from typing import ( # noqa: Y022 IO, Any, BinaryIO, - ByteString, ClassVar, Generic, Mapping, @@ -500,7 +499,7 @@ class str(Sequence[str]): def __rmul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc] def __getnewargs__(self) -> tuple[str]: ... -class bytes(ByteString): +class bytes(Sequence[int]): @overload def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ... @overload @@ -603,7 +602,9 @@ class bytes(ByteString): if sys.version_info >= (3, 11): def __bytes__(self) -> bytes: ... -class bytearray(MutableSequence[int], ByteString): + def __buffer__(self, __flags: int) -> memoryview: ... + +class bytearray(MutableSequence[int]): @overload def __init__(self) -> None: ... @overload @@ -718,6 +719,8 @@ class bytearray(MutableSequence[int], ByteString): def __gt__(self, __value: ReadableBuffer) -> bool: ... def __ge__(self, __value: ReadableBuffer) -> bool: ... def __alloc__(self) -> int: ... + def __buffer__(self, __flags: int) -> memoryview: ... + def __release_buffer__(self, __buffer: memoryview) -> None: ... @final class memoryview(Sequence[int]): @@ -779,6 +782,9 @@ class memoryview(Sequence[int]): else: def hex(self) -> str: ... + def __buffer__(self, __flags: int) -> memoryview: ... + def __release_buffer__(self, __buffer: memoryview) -> None: ... + @final class bool(int): def __new__(cls, __o: object = ...) -> Self: ... diff --git a/mypy/typeshed/stdlib/fcntl.pyi b/mypy/typeshed/stdlib/fcntl.pyi index 90676e365712..01443083f48d 100644 --- a/mypy/typeshed/stdlib/fcntl.pyi +++ b/mypy/typeshed/stdlib/fcntl.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import FileDescriptorLike, ReadOnlyBuffer, WriteableBuffer from typing import Any, overload -from typing_extensions import Literal +from typing_extensions import Buffer, Literal if sys.platform != "win32": FASYNC: int @@ -104,13 +104,19 @@ if sys.platform != "win32": def fcntl(__fd: FileDescriptorLike, __cmd: int, __arg: int = 0) -> int: ... @overload def fcntl(__fd: FileDescriptorLike, __cmd: int, __arg: str | ReadOnlyBuffer) -> bytes: ... + # If arg is an int, return int @overload def ioctl(__fd: FileDescriptorLike, __request: int, __arg: int = 0, __mutate_flag: bool = True) -> int: ... + # The return type works as follows: + # - If arg is a read-write buffer, return int if mutate_flag is True, otherwise bytes + # - If arg is a read-only buffer, return bytes (and ignore the value of mutate_flag) + # We can't represent that precisely as we can't distinguish between read-write and read-only + # buffers, so we add overloads for a few unambiguous cases and use Any for the rest. @overload - def ioctl(__fd: FileDescriptorLike, __request: int, __arg: WriteableBuffer, __mutate_flag: Literal[True] = True) -> int: ... + def ioctl(__fd: FileDescriptorLike, __request: int, __arg: bytes, __mutate_flag: bool = True) -> bytes: ... @overload def ioctl(__fd: FileDescriptorLike, __request: int, __arg: WriteableBuffer, __mutate_flag: Literal[False]) -> bytes: ... @overload - def ioctl(__fd: FileDescriptorLike, __request: int, __arg: ReadOnlyBuffer, __mutate_flag: bool = True) -> bytes: ... + def ioctl(__fd: FileDescriptorLike, __request: int, __arg: Buffer, __mutate_flag: bool = True) -> Any: ... def flock(__fd: FileDescriptorLike, __operation: int) -> None: ... def lockf(__fd: FileDescriptorLike, __cmd: int, __len: int = 0, __start: int = 0, __whence: int = 0) -> Any: ... diff --git a/mypy/typeshed/stdlib/fractions.pyi b/mypy/typeshed/stdlib/fractions.pyi index 3c84978c1bad..7ec8addeb136 100644 --- a/mypy/typeshed/stdlib/fractions.pyi +++ b/mypy/typeshed/stdlib/fractions.pyi @@ -22,11 +22,9 @@ else: class Fraction(Rational): @overload - def __new__( - cls, numerator: int | Rational = 0, denominator: int | Rational | None = None, *, _normalize: bool = True - ) -> Self: ... + def __new__(cls, numerator: int | Rational = 0, denominator: int | Rational | None = None) -> Self: ... @overload - def __new__(cls, __value: float | Decimal | str, *, _normalize: bool = True) -> Self: ... + def __new__(cls, __value: float | Decimal | str) -> Self: ... @classmethod def from_float(cls, f: float) -> Self: ... @classmethod @@ -34,6 +32,8 @@ class Fraction(Rational): def limit_denominator(self, max_denominator: int = 1000000) -> Fraction: ... if sys.version_info >= (3, 8): def as_integer_ratio(self) -> tuple[int, int]: ... + if sys.version_info >= (3, 12): + def is_integer(self) -> bool: ... @property def numerator(a) -> int: ... diff --git a/mypy/typeshed/stdlib/gzip.pyi b/mypy/typeshed/stdlib/gzip.pyi index 6a794f381ad6..1ec8b4b8ca7c 100644 --- a/mypy/typeshed/stdlib/gzip.pyi +++ b/mypy/typeshed/stdlib/gzip.pyi @@ -1,7 +1,7 @@ import _compression import sys import zlib -from _typeshed import ReadableBuffer, StrOrBytesPath, _BufferWithLen +from _typeshed import ReadableBuffer, SizedBuffer, StrOrBytesPath from io import FileIO from typing import Protocol, TextIO, overload from typing_extensions import Literal, TypeAlias @@ -159,9 +159,9 @@ class _GzipReader(_compression.DecompressReader): def __init__(self, fp: _ReadableFileobj) -> None: ... if sys.version_info >= (3, 8): - def compress(data: _BufferWithLen, compresslevel: int = 9, *, mtime: float | None = None) -> bytes: ... + def compress(data: SizedBuffer, compresslevel: int = 9, *, mtime: float | None = None) -> bytes: ... else: - def compress(data: _BufferWithLen, compresslevel: int = 9) -> bytes: ... + def compress(data: SizedBuffer, compresslevel: int = 9) -> bytes: ... def decompress(data: ReadableBuffer) -> bytes: ... diff --git a/mypy/typeshed/stdlib/hmac.pyi b/mypy/typeshed/stdlib/hmac.pyi index ee8af1b48d83..9ff99a5a05d5 100644 --- a/mypy/typeshed/stdlib/hmac.pyi +++ b/mypy/typeshed/stdlib/hmac.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import ReadableBuffer, _BufferWithLen +from _typeshed import ReadableBuffer, SizedBuffer from collections.abc import Callable from types import ModuleType from typing import Any, AnyStr, overload @@ -46,4 +46,4 @@ class HMAC: def compare_digest(__a: ReadableBuffer, __b: ReadableBuffer) -> bool: ... @overload def compare_digest(__a: AnyStr, __b: AnyStr) -> bool: ... -def digest(key: _BufferWithLen, msg: ReadableBuffer, digest: _DigestMod) -> bytes: ... +def digest(key: SizedBuffer, msg: ReadableBuffer, digest: _DigestMod) -> bytes: ... diff --git a/mypy/typeshed/stdlib/imaplib.pyi b/mypy/typeshed/stdlib/imaplib.pyi index 1c2112dd37c8..7781559c3888 100644 --- a/mypy/typeshed/stdlib/imaplib.pyi +++ b/mypy/typeshed/stdlib/imaplib.pyi @@ -1,7 +1,7 @@ import subprocess import sys import time -from _typeshed import ReadableBuffer, _BufferWithLen +from _typeshed import ReadableBuffer, SizedBuffer from builtins import list as _list # conflicts with a method named "list" from collections.abc import Callable from datetime import datetime @@ -155,7 +155,7 @@ class _Authenticator: def __init__(self, mechinst: Callable[[bytes], bytes | bytearray | memoryview | str | None]) -> None: ... def process(self, data: str) -> str: ... def encode(self, inp: bytes | bytearray | memoryview) -> str: ... - def decode(self, inp: str | _BufferWithLen) -> bytes: ... + def decode(self, inp: str | SizedBuffer) -> bytes: ... def Internaldate2tuple(resp: ReadableBuffer) -> time.struct_time | None: ... def Int2AP(num: SupportsAbs[SupportsInt]) -> bytes: ... diff --git a/mypy/typeshed/stdlib/inspect.pyi b/mypy/typeshed/stdlib/inspect.pyi index a2252e38ee8c..2d004a8e6b57 100644 --- a/mypy/typeshed/stdlib/inspect.pyi +++ b/mypy/typeshed/stdlib/inspect.pyi @@ -598,3 +598,25 @@ def classify_class_attrs(cls: type) -> list[Attribute]: ... if sys.version_info >= (3, 9): class ClassFoundException(Exception): ... + +if sys.version_info >= (3, 12): + class BufferFlags(enum.IntFlag): + SIMPLE: int + WRITABLE: int + FORMAT: int + ND: int + STRIDES: int + C_CONTIGUOUS: int + F_CONTIGUOUS: int + ANY_CONTIGUOUS: int + INDIRECT: int + CONTIG: int + CONTIG_RO: int + STRIDED: int + STRIDED_RO: int + RECORDS: int + RECORDS_RO: int + FULL: int + FULL_RO: int + READ: int + WRITE: int diff --git a/mypy/typeshed/stdlib/json/__init__.pyi b/mypy/typeshed/stdlib/json/__init__.pyi index 63e9718ee151..dc0cdff926d4 100644 --- a/mypy/typeshed/stdlib/json/__init__.pyi +++ b/mypy/typeshed/stdlib/json/__init__.pyi @@ -1,3 +1,4 @@ +import sys from _typeshed import SupportsRead, SupportsWrite from collections.abc import Callable from typing import Any @@ -6,6 +7,8 @@ from .decoder import JSONDecodeError as JSONDecodeError, JSONDecoder as JSONDeco from .encoder import JSONEncoder as JSONEncoder __all__ = ["dump", "dumps", "load", "loads", "JSONDecoder", "JSONDecodeError", "JSONEncoder"] +if sys.version_info >= (3, 12): + __all__ += ["AttrDict"] def dumps( obj: Any, @@ -59,3 +62,9 @@ def load( **kwds: Any, ) -> Any: ... def detect_encoding(b: bytes | bytearray) -> str: ... # undocumented + +if sys.version_info >= (3, 12): + class AttrDict(dict[str, Any]): + def __getattr__(self, name: str) -> Any: ... + def __setattr__(self, name: str, value: Any) -> None: ... + def __delattr__(self, name: str) -> None: ... diff --git a/mypy/typeshed/stdlib/logging/handlers.pyi b/mypy/typeshed/stdlib/logging/handlers.pyi index 3a7c8ade7498..ad5bf392b50f 100644 --- a/mypy/typeshed/stdlib/logging/handlers.pyi +++ b/mypy/typeshed/stdlib/logging/handlers.pyi @@ -179,7 +179,7 @@ class SysLogHandler(Handler): facility_names: ClassVar[dict[str, int]] # undocumented priority_map: ClassVar[dict[str, str]] # undocumented def __init__( - self, address: tuple[str, int] | str = ("localhost", 514), facility: int = 1, socktype: SocketKind | None = None + self, address: tuple[str, int] | str = ("localhost", 514), facility: str | int = 1, socktype: SocketKind | None = None ) -> None: ... if sys.version_info >= (3, 11): def createSocket(self) -> None: ... diff --git a/mypy/typeshed/stdlib/math.pyi b/mypy/typeshed/stdlib/math.pyi index 231964f397db..4a4d592b860d 100644 --- a/mypy/typeshed/stdlib/math.pyi +++ b/mypy/typeshed/stdlib/math.pyi @@ -112,7 +112,10 @@ def log1p(__x: _SupportsFloatOrIndex) -> float: ... def log2(__x: _SupportsFloatOrIndex) -> float: ... def modf(__x: _SupportsFloatOrIndex) -> tuple[float, float]: ... -if sys.version_info >= (3, 9): +if sys.version_info >= (3, 12): + def nextafter(__x: _SupportsFloatOrIndex, __y: _SupportsFloatOrIndex, *, steps: SupportsIndex | None = None) -> float: ... + +elif sys.version_info >= (3, 9): def nextafter(__x: _SupportsFloatOrIndex, __y: _SupportsFloatOrIndex) -> float: ... if sys.version_info >= (3, 8): @@ -130,6 +133,10 @@ def radians(__x: _SupportsFloatOrIndex) -> float: ... def remainder(__x: _SupportsFloatOrIndex, __y: _SupportsFloatOrIndex) -> float: ... def sin(__x: _SupportsFloatOrIndex) -> float: ... def sinh(__x: _SupportsFloatOrIndex) -> float: ... + +if sys.version_info >= (3, 12): + def sumprod(__p: Iterable[float], __q: Iterable[float]) -> float: ... + def sqrt(__x: _SupportsFloatOrIndex) -> float: ... def tan(__x: _SupportsFloatOrIndex) -> float: ... def tanh(__x: _SupportsFloatOrIndex) -> float: ... diff --git a/mypy/typeshed/stdlib/mmap.pyi b/mypy/typeshed/stdlib/mmap.pyi index 8da4ea7ca864..38e1924392c4 100644 --- a/mypy/typeshed/stdlib/mmap.pyi +++ b/mypy/typeshed/stdlib/mmap.pyi @@ -76,6 +76,8 @@ class mmap(Iterable[int], Sized): def __iter__(self) -> Iterator[int]: ... def __enter__(self) -> Self: ... def __exit__(self, *args: Unused) -> None: ... + def __buffer__(self, __flags: int) -> memoryview: ... + def __release_buffer__(self, __buffer: memoryview) -> None: ... if sys.version_info >= (3, 8) and sys.platform != "win32": MADV_NORMAL: int diff --git a/mypy/typeshed/stdlib/pickle.pyi b/mypy/typeshed/stdlib/pickle.pyi index 55ff38585b95..cf3995d74f5d 100644 --- a/mypy/typeshed/stdlib/pickle.pyi +++ b/mypy/typeshed/stdlib/pickle.pyi @@ -103,6 +103,8 @@ if sys.version_info >= (3, 8): def __init__(self, buffer: ReadableBuffer) -> None: ... def raw(self) -> memoryview: ... def release(self) -> None: ... + def __buffer__(self, __flags: int) -> memoryview: ... + def __release_buffer__(self, __buffer: memoryview) -> None: ... _BufferCallback: TypeAlias = Callable[[PickleBuffer], Any] | None def dump( obj: Any, diff --git a/mypy/typeshed/stdlib/random.pyi b/mypy/typeshed/stdlib/random.pyi index 5434f22407cc..9fd1c64f2bba 100644 --- a/mypy/typeshed/stdlib/random.pyi +++ b/mypy/typeshed/stdlib/random.pyi @@ -34,6 +34,8 @@ __all__ = [ if sys.version_info >= (3, 9): __all__ += ["randbytes"] +if sys.version_info >= (3, 12): + __all__ += ["binomialvariate"] _T = TypeVar("_T") @@ -79,8 +81,15 @@ class Random(_random.Random): def uniform(self, a: float, b: float) -> float: ... def triangular(self, low: float = 0.0, high: float = 1.0, mode: float | None = None) -> float: ... + if sys.version_info >= (3, 12): + def binomialvariate(self, n: int = 1, p: float = 0.5) -> int: ... + def betavariate(self, alpha: float, beta: float) -> float: ... - def expovariate(self, lambd: float) -> float: ... + if sys.version_info >= (3, 12): + def expovariate(self, lambd: float = 1.0) -> float: ... + else: + def expovariate(self, lambd: float) -> float: ... + def gammavariate(self, alpha: float, beta: float) -> float: ... if sys.version_info >= (3, 11): def gauss(self, mu: float = 0.0, sigma: float = 1.0) -> float: ... @@ -117,6 +126,8 @@ expovariate = _inst.expovariate vonmisesvariate = _inst.vonmisesvariate gammavariate = _inst.gammavariate gauss = _inst.gauss +if sys.version_info >= (3, 12): + binomialvariate = _inst.binomialvariate betavariate = _inst.betavariate paretovariate = _inst.paretovariate weibullvariate = _inst.weibullvariate diff --git a/mypy/typeshed/stdlib/shutil.pyi b/mypy/typeshed/stdlib/shutil.pyi index e8eb468337e1..ef716d4049dd 100644 --- a/mypy/typeshed/stdlib/shutil.pyi +++ b/mypy/typeshed/stdlib/shutil.pyi @@ -87,22 +87,46 @@ else: ignore_dangling_symlinks: bool = False, ) -> _PathReturn: ... -_OnErrorCallback: TypeAlias = Callable[[Callable[..., Any], Any, Any], object] +_OnErrorCallback: TypeAlias = Callable[[Callable[..., Any], str, Any], object] +_OnExcCallback: TypeAlias = Callable[[Callable[..., Any], str, Exception], object] class _RmtreeType(Protocol): avoids_symlink_attacks: bool - if sys.version_info >= (3, 11): + if sys.version_info >= (3, 12): + @overload def __call__( self, path: StrOrBytesPath, - ignore_errors: bool = ..., - onerror: _OnErrorCallback | None = ..., + ignore_errors: bool = False, + onerror: _OnErrorCallback | None = None, *, - dir_fd: int | None = ..., + onexc: None = None, + dir_fd: int | None = None, + ) -> None: ... + @overload + def __call__( + self, + path: StrOrBytesPath, + ignore_errors: bool = False, + onerror: None = None, + *, + onexc: _OnExcCallback, + dir_fd: int | None = None, + ) -> None: ... + elif sys.version_info >= (3, 11): + def __call__( + self, + path: StrOrBytesPath, + ignore_errors: bool = False, + onerror: _OnErrorCallback | None = None, + *, + dir_fd: int | None = None, ) -> None: ... else: - def __call__(self, path: StrOrBytesPath, ignore_errors: bool = ..., onerror: _OnErrorCallback | None = ...) -> None: ... + def __call__( + self, path: StrOrBytesPath, ignore_errors: bool = False, onerror: _OnErrorCallback | None = None + ) -> None: ... rmtree: _RmtreeType @@ -167,7 +191,15 @@ def register_archive_format( name: str, function: Callable[[str, str], object], extra_args: None = None, description: str = "" ) -> None: ... def unregister_archive_format(name: str) -> None: ... -def unpack_archive(filename: StrPath, extract_dir: StrPath | None = None, format: str | None = None) -> None: ... + +if sys.version_info >= (3, 12): + def unpack_archive( + filename: StrPath, extract_dir: StrPath | None = None, format: str | None = None, *, filter: str | None = None + ) -> None: ... + +else: + def unpack_archive(filename: StrPath, extract_dir: StrPath | None = None, format: str | None = None) -> None: ... + @overload def register_unpack_format( name: str, diff --git a/mypy/typeshed/stdlib/smtplib.pyi b/mypy/typeshed/stdlib/smtplib.pyi index 4228ad551eba..584fa164fec9 100644 --- a/mypy/typeshed/stdlib/smtplib.pyi +++ b/mypy/typeshed/stdlib/smtplib.pyi @@ -1,6 +1,6 @@ import sys from _socket import _Address as _SourceAddress -from _typeshed import ReadableBuffer, _BufferWithLen +from _typeshed import ReadableBuffer, SizedBuffer from collections.abc import Sequence from email.message import Message as _Message from re import Pattern @@ -133,7 +133,7 @@ class SMTP: self, from_addr: str, to_addrs: str | Sequence[str], - msg: _BufferWithLen | str, + msg: SizedBuffer | str, mail_options: Sequence[str] = (), rcpt_options: Sequence[str] = (), ) -> _SendErrs: ... diff --git a/mypy/typeshed/stdlib/statistics.pyi b/mypy/typeshed/stdlib/statistics.pyi index 1358b1f90d7d..af5fcec6ad0c 100644 --- a/mypy/typeshed/stdlib/statistics.pyi +++ b/mypy/typeshed/stdlib/statistics.pyi @@ -114,8 +114,15 @@ if sys.version_info >= (3, 8): def __rsub__(self, x2: float | NormalDist) -> NormalDist: ... __rmul__ = __mul__ -if sys.version_info >= (3, 10): +if sys.version_info >= (3, 12): + def correlation( + __x: Sequence[_Number], __y: Sequence[_Number], *, method: Literal["linear", "ranked"] = "linear" + ) -> float: ... + +elif sys.version_info >= (3, 10): def correlation(__x: Sequence[_Number], __y: Sequence[_Number]) -> float: ... + +if sys.version_info >= (3, 10): def covariance(__x: Sequence[_Number], __y: Sequence[_Number]) -> float: ... class LinearRegression(NamedTuple): diff --git a/mypy/typeshed/stdlib/typing.pyi b/mypy/typeshed/stdlib/typing.pyi index 6fc677dcbdc9..db042dc440ae 100644 --- a/mypy/typeshed/stdlib/typing.pyi +++ b/mypy/typeshed/stdlib/typing.pyi @@ -136,14 +136,32 @@ Any = object() @_final class TypeVar: - __name__: str - __bound__: Any | None - __constraints__: tuple[Any, ...] - __covariant__: bool - __contravariant__: bool - def __init__( - self, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False - ) -> None: ... + @property + def __name__(self) -> str: ... + @property + def __bound__(self) -> Any | None: ... + @property + def __constraints__(self) -> tuple[Any, ...]: ... + @property + def __covariant__(self) -> bool: ... + @property + def __contravariant__(self) -> bool: ... + if sys.version_info >= (3, 12): + @property + def __infer_variance__(self) -> bool: ... + def __init__( + self, + name: str, + *constraints: Any, + bound: Any | None = None, + covariant: bool = False, + contravariant: bool = False, + infer_variance: bool = False, + ) -> None: ... + else: + def __init__( + self, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False + ) -> None: ... if sys.version_info >= (3, 10): def __or__(self, right: Any) -> _SpecialForm: ... def __ror__(self, left: Any) -> _SpecialForm: ... @@ -193,30 +211,55 @@ if sys.version_info >= (3, 11): NotRequired: _SpecialForm LiteralString: _SpecialForm + @_final class TypeVarTuple: - __name__: str + @property + def __name__(self) -> str: ... def __init__(self, name: str) -> None: ... def __iter__(self) -> Any: ... def __typing_subst__(self, arg: Never) -> Never: ... def __typing_prepare_subst__(self, alias: Incomplete, args: Incomplete) -> Incomplete: ... if sys.version_info >= (3, 10): + @_final class ParamSpecArgs: - __origin__: ParamSpec + @property + def __origin__(self) -> ParamSpec: ... def __init__(self, origin: ParamSpec) -> None: ... + @_final class ParamSpecKwargs: - __origin__: ParamSpec + @property + def __origin__(self) -> ParamSpec: ... def __init__(self, origin: ParamSpec) -> None: ... + @_final class ParamSpec: - __name__: str - __bound__: Any | None - __covariant__: bool - __contravariant__: bool - def __init__( - self, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False - ) -> None: ... + @property + def __name__(self) -> str: ... + @property + def __bound__(self) -> Any | None: ... + @property + def __covariant__(self) -> bool: ... + @property + def __contravariant__(self) -> bool: ... + if sys.version_info >= (3, 12): + @property + def __infer_variance__(self) -> bool: ... + def __init__( + self, + name: str, + *, + bound: Any | None = None, + contravariant: bool = False, + covariant: bool = False, + infer_variance: bool = False, + ) -> None: ... + else: + def __init__( + self, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False + ) -> None: ... + @property def args(self) -> ParamSpecArgs: ... @property @@ -233,7 +276,7 @@ if sys.version_info >= (3, 10): class NewType: def __init__(self, name: str, tp: Any) -> None: ... - def __call__(self, x: _T) -> _T: ... + def __call__(self, __x: _T) -> _T: ... def __or__(self, other: Any) -> _SpecialForm: ... def __ror__(self, other: Any) -> _SpecialForm: ... __supertype__: type @@ -728,7 +771,7 @@ class TextIO(IO[str]): @abstractmethod def __enter__(self) -> TextIO: ... -class ByteString(Sequence[int], metaclass=ABCMeta): ... +ByteString: typing_extensions.TypeAlias = bytes | bytearray | memoryview # Functions @@ -800,18 +843,21 @@ if sys.version_info >= (3, 11): class NamedTuple(tuple[Any, ...]): if sys.version_info < (3, 8): - _field_types: collections.OrderedDict[str, type] + _field_types: ClassVar[collections.OrderedDict[str, type]] elif sys.version_info < (3, 9): - _field_types: dict[str, type] - _field_defaults: dict[str, Any] - _fields: tuple[str, ...] - _source: str + _field_types: ClassVar[dict[str, type]] + _field_defaults: ClassVar[dict[str, Any]] + _fields: ClassVar[tuple[str, ...]] + # __orig_bases__ sometimes exists on <3.12, but not consistently + # So we only add it to the stub on 3.12+. + if sys.version_info >= (3, 12): + __orig_bases__: ClassVar[tuple[Any, ...]] @overload def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ... @overload def __init__(self, typename: str, fields: None = None, **kwargs: Any) -> None: ... @classmethod - def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T: ... + def _make(cls, iterable: Iterable[Any]) -> typing_extensions.Self: ... if sys.version_info >= (3, 8): def _asdict(self) -> dict[str, Any]: ... else: @@ -827,6 +873,10 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta): if sys.version_info >= (3, 9): __required_keys__: ClassVar[frozenset[str]] __optional_keys__: ClassVar[frozenset[str]] + # __orig_bases__ sometimes exists on <3.12, but not consistently, + # so we only add it to the stub on 3.12+ + if sys.version_info >= (3, 12): + __orig_bases__: ClassVar[tuple[Any, ...]] def copy(self) -> typing_extensions.Self: ... # Using Never so that only calls using mypy plugin hook that specialize the signature # can go through. @@ -873,3 +923,26 @@ if sys.version_info >= (3, 10): def is_typeddict(tp: object) -> bool: ... def _type_repr(obj: object) -> str: ... + +if sys.version_info >= (3, 12): + def override(__arg: _F) -> _F: ... + @_final + class TypeAliasType: + def __init__( + self, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = () + ) -> None: ... + @property + def __value__(self) -> Any: ... + @property + def __type_params__(self) -> tuple[TypeVar | ParamSpec | TypeVarTuple, ...]: ... + @property + def __parameters__(self) -> tuple[Any, ...]: ... + @property + def __name__(self) -> str: ... + # It's writable on types, but not on instances of TypeAliasType. + @property + def __module__(self) -> str | None: ... # type: ignore[override] + def __getitem__(self, parameters: Any) -> Any: ... + if sys.version_info >= (3, 10): + def __or__(self, right: Any) -> _SpecialForm: ... + def __ror__(self, left: Any) -> _SpecialForm: ... diff --git a/mypy/typeshed/stdlib/typing_extensions.pyi b/mypy/typeshed/stdlib/typing_extensions.pyi index d567d8b96faf..93087a45a108 100644 --- a/mypy/typeshed/stdlib/typing_extensions.pyi +++ b/mypy/typeshed/stdlib/typing_extensions.pyi @@ -22,9 +22,14 @@ from typing import ( # noqa: Y022,Y039 DefaultDict as DefaultDict, Deque as Deque, Mapping, - NewType as NewType, NoReturn as NoReturn, Sequence, + SupportsAbs as SupportsAbs, + SupportsBytes as SupportsBytes, + SupportsComplex as SupportsComplex, + SupportsFloat as SupportsFloat, + SupportsInt as SupportsInt, + SupportsRound as SupportsRound, Text as Text, Type as Type, _Alias, @@ -39,6 +44,7 @@ if sys.version_info >= (3, 9): __all__ = [ "Any", + "Buffer", "ClassVar", "Concatenate", "Final", @@ -66,6 +72,12 @@ __all__ = [ "OrderedDict", "TypedDict", "SupportsIndex", + "SupportsAbs", + "SupportsRound", + "SupportsBytes", + "SupportsComplex", + "SupportsFloat", + "SupportsInt", "Annotated", "assert_never", "assert_type", @@ -84,6 +96,7 @@ __all__ = [ "runtime_checkable", "Text", "TypeAlias", + "TypeAliasType", "TypeGuard", "TYPE_CHECKING", "Never", @@ -93,6 +106,7 @@ __all__ = [ "clear_overloads", "get_args", "get_origin", + "get_original_bases", "get_overloads", "get_type_hints", ] @@ -134,6 +148,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta): __required_keys__: ClassVar[frozenset[str]] __optional_keys__: ClassVar[frozenset[str]] __total__: ClassVar[bool] + __orig_bases__: ClassVar[tuple[Any, ...]] def copy(self) -> Self: ... # Using Never so that only calls using mypy plugin hook that specialize the signature # can go through. @@ -183,10 +198,11 @@ class SupportsIndex(Protocol, metaclass=abc.ABCMeta): @abc.abstractmethod def __index__(self) -> int: ... -# New things in 3.10 +# New and changed things in 3.10 if sys.version_info >= (3, 10): from typing import ( Concatenate as Concatenate, + NewType as NewType, ParamSpecArgs as ParamSpecArgs, ParamSpecKwargs as ParamSpecKwargs, TypeAlias as TypeAlias, @@ -194,12 +210,16 @@ if sys.version_info >= (3, 10): is_typeddict as is_typeddict, ) else: + @final class ParamSpecArgs: - __origin__: ParamSpec + @property + def __origin__(self) -> ParamSpec: ... def __init__(self, origin: ParamSpec) -> None: ... + @final class ParamSpecKwargs: - __origin__: ParamSpec + @property + def __origin__(self) -> ParamSpec: ... def __init__(self, origin: ParamSpec) -> None: ... Concatenate: _SpecialForm @@ -207,6 +227,11 @@ else: TypeGuard: _SpecialForm def is_typeddict(tp: object) -> bool: ... + class NewType: + def __init__(self, name: str, tp: Any) -> None: ... + def __call__(self, __x: _T) -> _T: ... + __supertype__: type + # New things in 3.11 # NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11 if sys.version_info >= (3, 11): @@ -251,12 +276,12 @@ else: class NamedTuple(tuple[Any, ...]): if sys.version_info < (3, 8): - _field_types: collections.OrderedDict[str, type] + _field_types: ClassVar[collections.OrderedDict[str, type]] elif sys.version_info < (3, 9): - _field_types: dict[str, type] - _field_defaults: dict[str, Any] - _fields: tuple[str, ...] - _source: str + _field_types: ClassVar[dict[str, type]] + _field_defaults: ClassVar[dict[str, Any]] + _fields: ClassVar[tuple[str, ...]] + __orig_bases__: ClassVar[tuple[Any, ...]] @overload def __init__(self, typename: str, fields: Iterable[tuple[str, Any]] = ...) -> None: ... @overload @@ -272,16 +297,24 @@ else: # New things in 3.xx # The `default` parameter was added to TypeVar, ParamSpec, and TypeVarTuple (PEP 696) -# The `infer_variance` parameter was added to TypeVar (PEP 695) +# The `infer_variance` parameter was added to TypeVar in 3.12 (PEP 695) # typing_extensions.override (PEP 698) @final class TypeVar: - __name__: str - __bound__: Any | None - __constraints__: tuple[Any, ...] - __covariant__: bool - __contravariant__: bool - __default__: Any | None + @property + def __name__(self) -> str: ... + @property + def __bound__(self) -> Any | None: ... + @property + def __constraints__(self) -> tuple[Any, ...]: ... + @property + def __covariant__(self) -> bool: ... + @property + def __contravariant__(self) -> bool: ... + @property + def __infer_variance__(self) -> bool: ... + @property + def __default__(self) -> Any | None: ... def __init__( self, name: str, @@ -300,11 +333,18 @@ class TypeVar: @final class ParamSpec: - __name__: str - __bound__: type[Any] | None - __covariant__: bool - __contravariant__: bool - __default__: type[Any] | None + @property + def __name__(self) -> str: ... + @property + def __bound__(self) -> Any | None: ... + @property + def __covariant__(self) -> bool: ... + @property + def __contravariant__(self) -> bool: ... + @property + def __infer_variance__(self) -> bool: ... + @property + def __default__(self) -> Any | None: ... def __init__( self, name: str, @@ -321,10 +361,45 @@ class ParamSpec: @final class TypeVarTuple: - __name__: str - __default__: Any | None + @property + def __name__(self) -> str: ... + @property + def __default__(self) -> Any | None: ... def __init__(self, name: str, *, default: Any | None = None) -> None: ... def __iter__(self) -> Any: ... # Unpack[Self] -def override(__arg: _F) -> _F: ... def deprecated(__msg: str, *, category: type[Warning] | None = ..., stacklevel: int = 1) -> Callable[[_T], _T]: ... + +if sys.version_info >= (3, 12): + from collections.abc import Buffer as Buffer + from types import get_original_bases as get_original_bases + from typing import TypeAliasType as TypeAliasType, override as override +else: + def override(__arg: _F) -> _F: ... + def get_original_bases(__cls: type) -> tuple[Any, ...]: ... + @final + class TypeAliasType: + def __init__( + self, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = () + ) -> None: ... + @property + def __value__(self) -> Any: ... + @property + def __type_params__(self) -> tuple[TypeVar | ParamSpec | TypeVarTuple, ...]: ... + @property + def __parameters__(self) -> tuple[Any, ...]: ... + @property + def __name__(self) -> str: ... + # It's writable on types, but not on instances of TypeAliasType. + @property + def __module__(self) -> str | None: ... # type: ignore[override] + def __getitem__(self, parameters: Any) -> Any: ... + if sys.version_info >= (3, 10): + def __or__(self, right: Any) -> _SpecialForm: ... + def __ror__(self, left: Any) -> _SpecialForm: ... + + @runtime_checkable + class Buffer(Protocol): + # Not actually a Protocol at runtime; see + # https://github.com/python/typeshed/issues/10224 for why we're defining it this way + def __buffer__(self, __flags: int) -> memoryview: ... diff --git a/mypy/typeshed/stdlib/weakref.pyi b/mypy/typeshed/stdlib/weakref.pyi index 0bbab52f9b08..13f48fe85a8d 100644 --- a/mypy/typeshed/stdlib/weakref.pyi +++ b/mypy/typeshed/stdlib/weakref.pyi @@ -41,7 +41,7 @@ _P = ParamSpec("_P") ProxyTypes: tuple[type[Any], ...] class WeakMethod(ref[_CallableT], Generic[_CallableT]): - def __new__(cls, meth: _CallableT, callback: Callable[[_CallableT], object] | None = None) -> Self: ... + def __new__(cls, meth: _CallableT, callback: Callable[[Self], object] | None = None) -> Self: ... def __call__(self) -> _CallableT | None: ... def __eq__(self, other: object) -> bool: ... def __ne__(self, other: object) -> bool: ... diff --git a/mypy/typeshed/stdlib/xmlrpc/client.pyi b/mypy/typeshed/stdlib/xmlrpc/client.pyi index 8c32f3080749..79969359f091 100644 --- a/mypy/typeshed/stdlib/xmlrpc/client.pyi +++ b/mypy/typeshed/stdlib/xmlrpc/client.pyi @@ -2,7 +2,7 @@ import gzip import http.client import sys import time -from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite, _BufferWithLen +from _typeshed import ReadableBuffer, SizedBuffer, SupportsRead, SupportsWrite from collections.abc import Callable, Iterable, Mapping from datetime import datetime from io import BytesIO @@ -236,20 +236,20 @@ class Transport: def __init__(self, use_datetime: bool = False, use_builtin_types: bool = False) -> None: ... def request( - self, host: _HostType, handler: str, request_body: _BufferWithLen, verbose: bool = False + self, host: _HostType, handler: str, request_body: SizedBuffer, verbose: bool = False ) -> tuple[_Marshallable, ...]: ... def single_request( - self, host: _HostType, handler: str, request_body: _BufferWithLen, verbose: bool = False + self, host: _HostType, handler: str, request_body: SizedBuffer, verbose: bool = False ) -> tuple[_Marshallable, ...]: ... def getparser(self) -> tuple[ExpatParser, Unmarshaller]: ... def get_host_info(self, host: _HostType) -> tuple[str, list[tuple[str, str]], dict[str, str]]: ... def make_connection(self, host: _HostType) -> http.client.HTTPConnection: ... def close(self) -> None: ... def send_request( - self, host: _HostType, handler: str, request_body: _BufferWithLen, debug: bool + self, host: _HostType, handler: str, request_body: SizedBuffer, debug: bool ) -> http.client.HTTPConnection: ... def send_headers(self, connection: http.client.HTTPConnection, headers: list[tuple[str, str]]) -> None: ... - def send_content(self, connection: http.client.HTTPConnection, request_body: _BufferWithLen) -> None: ... + def send_content(self, connection: http.client.HTTPConnection, request_body: SizedBuffer) -> None: ... def parse_response(self, response: http.client.HTTPResponse) -> tuple[_Marshallable, ...]: ... class SafeTransport(Transport): diff --git a/mypy/typeshed/stdlib/zipfile.pyi b/mypy/typeshed/stdlib/zipfile.pyi index 92f1dc49adbc..abda7a3b9625 100644 --- a/mypy/typeshed/stdlib/zipfile.pyi +++ b/mypy/typeshed/stdlib/zipfile.pyi @@ -1,6 +1,6 @@ import io import sys -from _typeshed import StrOrBytesPath, StrPath, _BufferWithLen +from _typeshed import SizedBuffer, StrOrBytesPath, StrPath from collections.abc import Callable, Iterable, Iterator from os import PathLike from types import TracebackType @@ -179,7 +179,7 @@ class ZipFile: def writestr( self, zinfo_or_arcname: str | ZipInfo, - data: _BufferWithLen | str, + data: SizedBuffer | str, compress_type: int | None = None, compresslevel: int | None = None, ) -> None: ... diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 760a761705f4..7108734102d1 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1019,7 +1019,7 @@ re.subn(bpat, lambda m: b'', b'')[0] + b'' _testReModuleBytes.py:7: error: No overload variant of "search" matches argument types "bytes", "str" _testReModuleBytes.py:7: note: Possible overload variants: _testReModuleBytes.py:7: note: def search(pattern: Union[str, Pattern[str]], string: str, flags: Union[int, RegexFlag] = ...) -> Optional[Match[str]] -_testReModuleBytes.py:7: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData]], flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]] +_testReModuleBytes.py:7: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Buffer, flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]] _testReModuleBytes.py:9: error: Argument 1 to "search" has incompatible type "Pattern[bytes]"; expected "Union[str, Pattern[str]]" [case testReModuleString] @@ -1046,7 +1046,7 @@ re.subn(spat, lambda m: '', '')[0] + '' _testReModuleString.py:7: error: No overload variant of "search" matches argument types "str", "bytes" _testReModuleString.py:7: note: Possible overload variants: _testReModuleString.py:7: note: def search(pattern: Union[str, Pattern[str]], string: str, flags: Union[int, RegexFlag] = ...) -> Optional[Match[str]] -_testReModuleString.py:7: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Union[bytes, Union[bytearray, memoryview, array[Any], mmap, _CData]], flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]] +_testReModuleString.py:7: note: def search(pattern: Union[bytes, Pattern[bytes]], string: Buffer, flags: Union[int, RegexFlag] = ...) -> Optional[Match[bytes]] _testReModuleString.py:9: error: Argument 1 to "search" has incompatible type "Pattern[str]"; expected "Union[bytes, Pattern[bytes]]" [case testListSetitemTuple]