Skip to content

Commit

Permalink
Replace SupportsGetItem
Browse files Browse the repository at this point in the history
Part of python#11822
  • Loading branch information
srittau committed Apr 24, 2024
1 parent 29db988 commit 9c80d43
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions stdlib/_operator.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 14 additions & 1 deletion stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Expand Down
6 changes: 3 additions & 3 deletions stdlib/cgi.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions stdlib/lib2to3/btm_matcher.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from _typeshed import Incomplete, SupportsGetItem
from _typeshed import Incomplete, TempSupportsGetItem
from collections import defaultdict
from collections.abc import Iterable

Expand All @@ -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: ...

Expand Down
6 changes: 3 additions & 3 deletions stdlib/lib2to3/pytree.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: ...
Expand All @@ -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]]: ...
4 changes: 2 additions & 2 deletions stdlib/lib2to3/refactor.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: ...
Expand Down

0 comments on commit 9c80d43

Please sign in to comment.