Skip to content

Sync typeshed #18880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ def generate_property_stub(

def get_type_fullname(self, typ: type) -> str:
"""Given a type, return a string representation"""
if typ is Any: # type: ignore[comparison-overlap]
if typ is Any:
return "Any"
typename = getattr(typ, "__qualname__", typ.__name__)
module_name = self.get_obj_module(typ)
Expand Down
5 changes: 3 additions & 2 deletions mypy/typeshed/stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# See the README.md file in this directory for more information.

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

# Superset of typing.AnyStr that also includes LiteralString
AnyOrLiteralStr = TypeVar("AnyOrLiteralStr", str, bytes, LiteralString) # noqa: Y001
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/asyncio/sslproto.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class _SSLProtocolTransport(transports._FlowControlMixin, transports.Transport):
def get_extra_info(self, name: str, default: Any | None = None) -> dict[str, Any]: ...
@property
def _protocol_paused(self) -> bool: ...
def write(self, data: bytes | bytearray | memoryview) -> None: ...
def write(self, data: bytes | bytearray | memoryview[Any]) -> None: ... # any memoryview format or shape
def can_write_eof(self) -> Literal[False]: ...
if sys.version_info >= (3, 11):
def get_write_buffer_limits(self) -> tuple[int, int]: ...
Expand Down
6 changes: 4 additions & 2 deletions mypy/typeshed/stdlib/asyncio/transports.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class WriteTransport(BaseTransport):
def set_write_buffer_limits(self, high: int | None = None, low: int | None = None) -> None: ...
def get_write_buffer_size(self) -> int: ...
def get_write_buffer_limits(self) -> tuple[int, int]: ...
def write(self, data: bytes | bytearray | memoryview) -> None: ...
def writelines(self, list_of_data: Iterable[bytes | bytearray | memoryview]) -> None: ...
def write(self, data: bytes | bytearray | memoryview[Any]) -> None: ... # any memoryview format or shape
def writelines(
self, list_of_data: Iterable[bytes | bytearray | memoryview[Any]]
) -> None: ... # any memoryview format or shape
def write_eof(self) -> None: ...
def can_write_eof(self) -> bool: ...
def abort(self) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeshed/stdlib/heapq.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from _heapq import *
from _typeshed import SupportsRichComparison
from collections.abc import Callable, Iterable
from collections.abc import Callable, Generator, Iterable
from typing import Any, Final, TypeVar

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

def merge(
*iterables: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None, reverse: bool = False
) -> Iterable[_S]: ...
) -> Generator[_S]: ...
def nlargest(n: int, iterable: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None) -> list[_S]: ...
def nsmallest(n: int, iterable: Iterable[_S], key: Callable[[_S], SupportsRichComparison] | None = None) -> list[_S]: ...
def _heapify_max(heap: list[Any], /) -> None: ... # undocumented
3 changes: 2 additions & 1 deletion mypy/typeshed/stdlib/http/server.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import sys
from _typeshed import StrPath, SupportsRead, SupportsWrite
from collections.abc import Mapping, Sequence
from typing import Any, AnyStr, BinaryIO, ClassVar
from typing_extensions import deprecated

__all__ = ["HTTPServer", "ThreadingHTTPServer", "BaseHTTPRequestHandler", "SimpleHTTPRequestHandler", "CGIHTTPRequestHandler"]

Expand Down Expand Up @@ -72,7 +73,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def guess_type(self, path: StrPath) -> str: ... # undocumented

def executable(path: StrPath) -> bool: ... # undocumented

@deprecated("Deprecated in Python 3.13; removal scheduled for Python 3.15")
class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
cgi_directories: list[str]
have_fork: bool # undocumented
Expand Down
6 changes: 3 additions & 3 deletions mypy/typeshed/stdlib/importlib/resources/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from collections.abc import Iterator
from contextlib import AbstractContextManager
from pathlib import Path
from types import ModuleType
from typing import Any, BinaryIO, TextIO
from typing import Any, BinaryIO, Literal, TextIO
from typing_extensions import TypeAlias

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -51,14 +51,14 @@ else:
def open_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> TextIO: ...
def read_binary(package: Package, resource: Resource) -> bytes: ...
def read_text(package: Package, resource: Resource, encoding: str = "utf-8", errors: str = "strict") -> str: ...
def path(package: Package, resource: Resource) -> AbstractContextManager[Path]: ...
def path(package: Package, resource: Resource) -> AbstractContextManager[Path, Literal[False]]: ...
def is_resource(package: Package, name: str) -> bool: ...
def contents(package: Package) -> Iterator[str]: ...

if sys.version_info >= (3, 11):
from importlib.resources._common import as_file as as_file
elif sys.version_info >= (3, 9):
def as_file(path: Traversable) -> AbstractContextManager[Path]: ...
def as_file(path: Traversable) -> AbstractContextManager[Path, Literal[False]]: ...

if sys.version_info >= (3, 11):
from importlib.resources._common import files as files
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeshed/stdlib/importlib/resources/_common.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if sys.version_info >= (3, 11):
from contextlib import AbstractContextManager
from importlib.abc import ResourceReader, Traversable
from pathlib import Path
from typing import overload
from typing import Literal, overload
from typing_extensions import TypeAlias, deprecated

Package: TypeAlias = str | types.ModuleType
Expand Down Expand Up @@ -39,4 +39,4 @@ if sys.version_info >= (3, 11):
def get_package(package: Package) -> types.ModuleType: ...

def from_package(package: types.ModuleType) -> Traversable: ...
def as_file(path: Traversable) -> AbstractContextManager[Path]: ...
def as_file(path: Traversable) -> AbstractContextManager[Path, Literal[False]]: ...
4 changes: 2 additions & 2 deletions mypy/typeshed/stdlib/importlib/resources/_functional.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if sys.version_info >= (3, 13):
from importlib.resources._common import Anchor
from io import TextIOWrapper
from pathlib import Path
from typing import BinaryIO, overload
from typing import BinaryIO, Literal, overload
from typing_extensions import Unpack

def open_binary(anchor: Anchor, *path_names: StrPath) -> BinaryIO: ...
Expand All @@ -25,6 +25,6 @@ if sys.version_info >= (3, 13):
) -> str: ...
@overload
def read_text(anchor: Anchor, *path_names: StrPath, encoding: str | None, errors: str | None = "strict") -> str: ...
def path(anchor: Anchor, *path_names: StrPath) -> AbstractContextManager[Path]: ...
def path(anchor: Anchor, *path_names: StrPath) -> AbstractContextManager[Path, Literal[False]]: ...
def is_resource(anchor: Anchor, *path_names: StrPath) -> bool: ...
def contents(anchor: Anchor, *path_names: StrPath) -> Iterator[str]: ...
8 changes: 4 additions & 4 deletions mypy/typeshed/stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ class Signature:

if sys.version_info >= (3, 10):
def get_annotations(
obj: Callable[..., object] | type[Any] | ModuleType,
obj: Callable[..., object] | type[object] | ModuleType, # any callable, class, or module
*,
globals: Mapping[str, Any] | None = None,
locals: Mapping[str, Any] | None = None,
globals: Mapping[str, Any] | None = None, # value types depend on the key
locals: Mapping[str, Any] | None = None, # value types depend on the key
eval_str: bool = False,
) -> dict[str, Any]: ...
) -> dict[str, Any]: ... # values are type expressions

# The name is the same as the enum's name in CPython
class _ParameterKind(enum.IntEnum):
Expand Down
7 changes: 6 additions & 1 deletion mypy/typeshed/stdlib/logging/config.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
from configparser import RawConfigParser
from re import Pattern
from threading import Thread
from typing import IO, Any, Final, Literal, SupportsIndex, TypedDict, overload
from typing import IO, Any, Final, Literal, SupportsIndex, TypedDict, overload, type_check_only
from typing_extensions import Required, TypeAlias

from . import Filter, Filterer, Formatter, Handler, Logger, _FilterType, _FormatStyle, _Level
Expand All @@ -14,24 +14,28 @@ RESET_ERROR: Final[int] # undocumented
IDENTIFIER: Final[Pattern[str]] # undocumented

if sys.version_info >= (3, 11):
@type_check_only
class _RootLoggerConfiguration(TypedDict, total=False):
level: _Level
filters: Sequence[str | _FilterType]
handlers: Sequence[str]

else:
@type_check_only
class _RootLoggerConfiguration(TypedDict, total=False):
level: _Level
filters: Sequence[str]
handlers: Sequence[str]

@type_check_only
class _LoggerConfiguration(_RootLoggerConfiguration, TypedDict, total=False):
propagate: bool

_FormatterConfigurationTypedDict = TypedDict(
"_FormatterConfigurationTypedDict", {"class": str, "format": str, "datefmt": str, "style": _FormatStyle}, total=False
)

@type_check_only
class _FilterConfigurationTypedDict(TypedDict):
name: str

Expand All @@ -43,6 +47,7 @@ _FilterConfiguration: TypeAlias = _FilterConfigurationTypedDict | dict[str, Any]
# Handler config can have additional keys even when not providing a custom factory so we just use `dict`.
_HandlerConfiguration: TypeAlias = dict[str, Any]

@type_check_only
class _DictConfigArgs(TypedDict, total=False):
version: Required[Literal[1]]
formatters: dict[str, _FormatterConfiguration]
Expand Down
12 changes: 6 additions & 6 deletions mypy/typeshed/stdlib/pkgutil.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import SupportsRead
from _typeshed import StrOrBytesPath, SupportsRead
from _typeshed.importlib import LoaderProtocol, MetaPathFinderProtocol, PathEntryFinderProtocol
from collections.abc import Callable, Iterable, Iterator
from typing import IO, Any, NamedTuple, TypeVar
Expand Down Expand Up @@ -31,21 +31,21 @@ def extend_path(path: _PathT, name: str) -> _PathT: ...

if sys.version_info < (3, 12):
class ImpImporter:
def __init__(self, path: str | None = None) -> None: ...
def __init__(self, path: StrOrBytesPath | None = None) -> None: ...

class ImpLoader:
def __init__(self, fullname: str, file: IO[str], filename: str, etc: tuple[str, str, int]) -> None: ...
def __init__(self, fullname: str, file: IO[str], filename: StrOrBytesPath, etc: tuple[str, str, int]) -> None: ...

@deprecated("Use importlib.util.find_spec() instead. Will be removed in Python 3.14.")
def find_loader(fullname: str) -> LoaderProtocol | None: ...
def get_importer(path_item: str) -> PathEntryFinderProtocol | None: ...
def get_importer(path_item: StrOrBytesPath) -> PathEntryFinderProtocol | None: ...
@deprecated("Use importlib.util.find_spec() instead. Will be removed in Python 3.14.")
def get_loader(module_or_name: str) -> LoaderProtocol | None: ...
def iter_importers(fullname: str = "") -> Iterator[MetaPathFinderProtocol | PathEntryFinderProtocol]: ...
def iter_modules(path: Iterable[str] | None = None, prefix: str = "") -> Iterator[ModuleInfo]: ...
def iter_modules(path: Iterable[StrOrBytesPath] | None = None, prefix: str = "") -> Iterator[ModuleInfo]: ...
def read_code(stream: SupportsRead[bytes]) -> Any: ... # undocumented
def walk_packages(
path: Iterable[str] | None = None, prefix: str = "", onerror: Callable[[str], object] | None = None
path: Iterable[StrOrBytesPath] | None = None, prefix: str = "", onerror: Callable[[str], object] | None = None
) -> Iterator[ModuleInfo]: ...
def get_data(package: str, resource: str) -> bytes | None: ...

Expand Down
29 changes: 14 additions & 15 deletions mypy/typeshed/stdlib/statistics.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from _typeshed import SupportsRichComparisonT
from collections.abc import Callable, Hashable, Iterable, Sequence
from decimal import Decimal
from fractions import Fraction
from typing import Any, Literal, NamedTuple, SupportsFloat, TypeVar
from typing import Literal, NamedTuple, SupportsFloat, SupportsIndex, TypeVar
from typing_extensions import Self, TypeAlias

__all__ = [
Expand Down Expand Up @@ -38,6 +38,9 @@ _NumberT = TypeVar("_NumberT", float, Decimal, Fraction)
# Used in mode, multimode
_HashableT = TypeVar("_HashableT", bound=Hashable)

# Used in NormalDist.samples and kde_random
_Seed: TypeAlias = int | float | str | bytes | bytearray # noqa: Y041

class StatisticsError(ValueError): ...

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -89,7 +92,7 @@ class NormalDist:
def variance(self) -> float: ...
@classmethod
def from_samples(cls, data: Iterable[SupportsFloat]) -> Self: ...
def samples(self, n: int, *, seed: Any | None = None) -> list[float]: ...
def samples(self, n: SupportsIndex, *, seed: _Seed | None = None) -> list[float]: ...
def pdf(self, x: float) -> float: ...
def cdf(self, x: float) -> float: ...
def inv_cdf(self, p: float) -> float: ...
Expand All @@ -98,15 +101,15 @@ class NormalDist:
if sys.version_info >= (3, 9):
def zscore(self, x: float) -> float: ...

def __eq__(self, x2: object) -> bool: ...
def __add__(self, x2: float | NormalDist) -> NormalDist: ...
def __sub__(self, x2: float | NormalDist) -> NormalDist: ...
def __mul__(self, x2: float) -> NormalDist: ...
def __truediv__(self, x2: float) -> NormalDist: ...
def __pos__(self) -> NormalDist: ...
def __neg__(self) -> NormalDist: ...
def __eq__(x1, x2: object) -> bool: ...
def __add__(x1, x2: float | NormalDist) -> NormalDist: ...
def __sub__(x1, x2: float | NormalDist) -> NormalDist: ...
def __mul__(x1, x2: float) -> NormalDist: ...
def __truediv__(x1, x2: float) -> NormalDist: ...
def __pos__(x1) -> NormalDist: ...
def __neg__(x1) -> NormalDist: ...
__radd__ = __add__
def __rsub__(self, x2: float | NormalDist) -> NormalDist: ...
def __rsub__(x1, x2: float | NormalDist) -> NormalDist: ...
__rmul__ = __mul__
def __hash__(self) -> int: ...

Expand Down Expand Up @@ -153,9 +156,5 @@ if sys.version_info >= (3, 13):
data: Sequence[float], h: float, kernel: _Kernel = "normal", *, cumulative: bool = False
) -> Callable[[float], float]: ...
def kde_random(
data: Sequence[float],
h: float,
kernel: _Kernel = "normal",
*,
seed: int | float | str | bytes | bytearray | None = None, # noqa: Y041
data: Sequence[float], h: float, kernel: _Kernel = "normal", *, seed: _Seed | None = None
) -> Callable[[], float]: ...
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/tkinter/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ else:
GraphicsExpose = "13"
Gravity = "24"
KeyPress = "2"
Key = "2"
Key = KeyPress
KeyRelease = "3"
Keymap = "11"
Leave = "8"
Expand Down
2 changes: 2 additions & 0 deletions mypy/typeshed/stdlib/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ if sys.version_info >= (3, 10):
class UnionType:
@property
def __args__(self) -> tuple[Any, ...]: ...
@property
def __parameters__(self) -> tuple[Any, ...]: ...
def __or__(self, value: Any, /) -> UnionType: ...
def __ror__(self, value: Any, /) -> UnionType: ...
def __eq__(self, value: object, /) -> bool: ...
Expand Down
8 changes: 5 additions & 3 deletions mypy/typeshed/stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ if sys.version_info >= (3, 12):
if sys.version_info >= (3, 13):
__all__ += ["get_protocol_members", "is_protocol", "NoDefault", "TypeIs", "ReadOnly"]

Any = object()

class Any: ...
class _Final: ...

def final(f: _T) -> _T: ...
Expand Down Expand Up @@ -950,14 +949,17 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
# so we only add it to the stub on 3.12+
if sys.version_info >= (3, 12):
__orig_bases__: ClassVar[tuple[Any, ...]]
if sys.version_info >= (3, 13):
__readonly_keys__: ClassVar[frozenset[str]]
__mutable_keys__: ClassVar[frozenset[str]]

def copy(self) -> typing_extensions.Self: ...
# Using Never so that only calls using mypy plugin hook that specialize the signature
# can go through.
def setdefault(self, k: _Never, default: object) -> object: ...
# Mypy plugin hook for 'pop' expects that 'default' has a type variable type.
def pop(self, k: _Never, default: _T = ...) -> object: ... # pyright: ignore[reportInvalidTypeVarUse]
def update(self: _T, m: _T, /) -> None: ...
def update(self, m: typing_extensions.Self, /) -> None: ...
def __delitem__(self, k: _Never) -> None: ...
def items(self) -> dict_items[str, object]: ...
def keys(self) -> dict_keys[str, object]: ...
Expand Down
Loading