From 9c80d4374ceb8837940b5f7a5e021c6dc61a70bc Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 24 Apr 2024 11:56:26 +0200 Subject: [PATCH] Replace SupportsGetItem Part of #11822 --- stdlib/_operator.pyi | 6 +++--- stdlib/_typeshed/__init__.pyi | 15 ++++++++++++++- stdlib/cgi.pyi | 6 +++--- stdlib/lib2to3/btm_matcher.pyi | 4 ++-- stdlib/lib2to3/pytree.pyi | 6 +++--- stdlib/lib2to3/refactor.pyi | 4 ++-- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/stdlib/_operator.pyi b/stdlib/_operator.pyi index 69ee563b5cf4..00e77cc9e4dd 100644 --- a/stdlib/_operator.pyi +++ b/stdlib/_operator.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import SupportsGetItem +from _typeshed import TempSupportsGetItem from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, SupportsIndex, TypeVar, final, overload from typing_extensions import ParamSpec, TypeAlias, TypeVarTuple, Unpack @@ -83,7 +83,7 @@ def delitem(a: MutableMapping[_K, Any], b: _K, /) -> None: ... @overload def getitem(a: Sequence[_T], b: slice, /) -> Sequence[_T]: ... @overload -def getitem(a: SupportsGetItem[_K, _V], b: _K, /) -> _V: ... +def getitem(a: TempSupportsGetItem[_K, _V], b: _K, /) -> _V: ... def indexOf(a: Iterable[_T], b: _T, /) -> int: ... @overload def setitem(a: MutableSequence[_T], b: SupportsIndex, c: _T, /) -> None: ... @@ -119,7 +119,7 @@ class itemgetter(Generic[_T_co]): # # A suspected mypy issue prevents using [..., _T] instead of [..., Any] here. # https://github.com/python/mypy/issues/14032 - def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ... + def __call__(self, obj: TempSupportsGetItem[Any, Any]) -> Any: ... @final class methodcaller: diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index db143e42519a..e4a9f9297a20 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -151,13 +151,26 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]): def keys(self) -> Iterable[_KT]: ... def __getitem__(self, key: _KT, /) -> _VT_co: ... +# Temporary placeholder, until the SupportsGetItem name becomes available. +# Will be removed after that. +class TempSupportsGetItem(Protocol[_KT_contra, _VT_co]): + def __getitem__(self, key: _KT_contra, /) -> _VT_co: ... + # stable +class SupportsGetItemAndContains(Protocol[_KT_contra, _VT_co]): + def __contains__(self, x: Any, /) -> bool: ... + def __getitem__(self, key: _KT_contra, /) -> _VT_co: ... + +# DEPRECATED: Use SupportsGetItemAndContains or TempSupportsGetItem instead, +# as appropriate. Will be removed May 2025. class SupportsGetItem(Protocol[_KT_contra, _VT_co]): def __contains__(self, x: Any, /) -> bool: ... def __getitem__(self, key: _KT_contra, /) -> _VT_co: ... # stable -class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, _VT]): +class SupportsItemAccess(Protocol[_KT_contra, _VT]): + def __contains__(self, x: Any, /) -> bool: ... + def __getitem__(self, key: _KT_contra, /) -> _VT: ... def __setitem__(self, key: _KT_contra, value: _VT, /) -> None: ... def __delitem__(self, key: _KT_contra, /) -> None: ... diff --git a/stdlib/cgi.pyi b/stdlib/cgi.pyi index d20be33e3d76..36bf138bae5c 100644 --- a/stdlib/cgi.pyi +++ b/stdlib/cgi.pyi @@ -1,4 +1,4 @@ -from _typeshed import SupportsGetItem, SupportsItemAccess, Unused +from _typeshed import SupportsGetItemAndContains, SupportsItemAccess, TempSupportsGetItem, Unused from builtins import list as _list, type as _type from collections.abc import Iterable, Iterator, Mapping from email.message import Message @@ -29,7 +29,7 @@ def parse( separator: str = "&", ) -> dict[str, list[str]]: ... def parse_multipart( - fp: IO[Any], pdict: SupportsGetItem[str, bytes], encoding: str = "utf-8", errors: str = "replace", separator: str = "&" + fp: IO[Any], pdict: TempSupportsGetItem[str, bytes], encoding: str = "utf-8", errors: str = "replace", separator: str = "&" ) -> dict[str, list[Any]]: ... class _Environ(Protocol): @@ -85,7 +85,7 @@ class FieldStorage: fp: IO[Any] | None = None, headers: Mapping[str, str] | Message | None = None, outerboundary: bytes = b"", - environ: SupportsGetItem[str, str] = ..., + environ: SupportsGetItemAndContains[str, str] = ..., keep_blank_values: int = 0, strict_parsing: int = 0, limit: int | None = None, diff --git a/stdlib/lib2to3/btm_matcher.pyi b/stdlib/lib2to3/btm_matcher.pyi index 4c87b664eb20..635e6225ac72 100644 --- a/stdlib/lib2to3/btm_matcher.pyi +++ b/stdlib/lib2to3/btm_matcher.pyi @@ -1,4 +1,4 @@ -from _typeshed import Incomplete, SupportsGetItem +from _typeshed import Incomplete, TempSupportsGetItem from collections import defaultdict from collections.abc import Iterable @@ -21,7 +21,7 @@ class BottomMatcher: logger: Incomplete def __init__(self) -> None: ... def add_fixer(self, fixer: BaseFix) -> None: ... - def add(self, pattern: SupportsGetItem[int | slice, Incomplete] | None, start: BMNode) -> list[BMNode]: ... + def add(self, pattern: TempSupportsGetItem[int | slice, Incomplete] | None, start: BMNode) -> list[BMNode]: ... def run(self, leaves: Iterable[Leaf]) -> defaultdict[BaseFix, list[Node | Leaf]]: ... def print_ac(self) -> None: ... diff --git a/stdlib/lib2to3/pytree.pyi b/stdlib/lib2to3/pytree.pyi index 138333bd58af..3025ecf50397 100644 --- a/stdlib/lib2to3/pytree.pyi +++ b/stdlib/lib2to3/pytree.pyi @@ -1,4 +1,4 @@ -from _typeshed import Incomplete, SupportsGetItem, SupportsLenAndGetItem, Unused +from _typeshed import Incomplete, SupportsLenAndGetItem, TempSupportsGetItem, Unused from abc import abstractmethod from collections.abc import Iterable, Iterator, MutableSequence from typing import Final @@ -95,7 +95,7 @@ class BasePattern: def optimize(self) -> BasePattern: ... # sic, subclasses are free to optimize themselves into different patterns def match(self, node: _NL, results: _Results | None = None) -> bool: ... def match_seq(self, nodes: SupportsLenAndGetItem[_NL], results: _Results | None = None) -> bool: ... - def generate_matches(self, nodes: SupportsGetItem[int, _NL]) -> Iterator[tuple[int, _Results]]: ... + def generate_matches(self, nodes: TempSupportsGetItem[int, _NL]) -> Iterator[tuple[int, _Results]]: ... class LeafPattern(BasePattern): def __init__(self, type: int | None = None, content: str | None = None, name: str | None = None) -> None: ... @@ -113,5 +113,5 @@ class NegatedPattern(BasePattern): def __init__(self, content: str | None = None) -> None: ... def generate_matches( - patterns: SupportsGetItem[int | slice, BasePattern] | None, nodes: SupportsGetItem[int | slice, _NL] + patterns: TempSupportsGetItem[int | slice, BasePattern] | None, nodes: TempSupportsGetItem[int | slice, _NL] ) -> Iterator[tuple[int, _Results]]: ... diff --git a/stdlib/lib2to3/refactor.pyi b/stdlib/lib2to3/refactor.pyi index a7f382540648..5e76d947ce49 100644 --- a/stdlib/lib2to3/refactor.pyi +++ b/stdlib/lib2to3/refactor.pyi @@ -1,4 +1,4 @@ -from _typeshed import FileDescriptorOrPath, StrPath, SupportsGetItem +from _typeshed import FileDescriptorOrPath, StrPath, TempSupportsGetItem from collections.abc import Container, Generator, Iterable, Mapping from logging import Logger, _ExcInfoType from multiprocessing import JoinableQueue @@ -56,7 +56,7 @@ class RefactoringTool: def refactor_string(self, data: str, name: str) -> Node | None: ... def refactor_stdin(self, doctests_only: bool = False) -> None: ... def refactor_tree(self, tree: Node, name: str) -> bool: ... - def traverse_by(self, fixers: SupportsGetItem[int, Iterable[BaseFix]] | None, traversal: Iterable[Node]) -> None: ... + def traverse_by(self, fixers: TempSupportsGetItem[int, Iterable[BaseFix]] | None, traversal: Iterable[Node]) -> None: ... def processed_file( self, new_text: str, filename: StrPath, old_text: str | None = None, write: bool = False, encoding: str | None = None ) -> None: ...