Improve accuracy of six byte index methods (#9117)

This commit is contained in:
Samuel T
2022-11-09 22:22:33 -05:00
committed by GitHub
parent 0ac98f6b80
commit 796bdc2eb0
6 changed files with 41 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
import sys
from collections.abc import Callable, Container, Iterable, Mapping, MutableMapping, MutableSequence, Sequence
from _typeshed import SupportsGetItem
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, TypeVar, overload
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, final
@@ -77,11 +78,9 @@ def delitem(__a: MutableSequence[Any], __b: slice) -> None: ...
@overload
def delitem(__a: MutableMapping[_K, Any], __b: _K) -> None: ...
@overload
def getitem(__a: Sequence[_T], __b: SupportsIndex) -> _T: ...
@overload
def getitem(__a: Sequence[_T], __b: slice) -> Sequence[_T]: ...
@overload
def getitem(__a: Mapping[_K, _V], __b: _K) -> _V: ...
def getitem(__a: SupportsGetItem[_K, _V], __b: _K) -> _V: ...
def indexOf(__a: Iterable[_T], __b: _T) -> int: ...
@overload
def setitem(__a: MutableSequence[_T], __b: SupportsIndex, __c: _T) -> None: ...
@@ -106,17 +105,30 @@ class attrgetter(Generic[_T_co]):
@final
class itemgetter(Generic[_T_co]):
# mypy lacks support for PEP 646 https://github.com/python/mypy/issues/12280
# So we have to define all of these overloads to simulate unpacking the arguments
@overload
def __new__(cls, item: Any) -> itemgetter[Any]: ...
def __new__(cls, item: _T_co) -> itemgetter[_T_co]: ...
@overload
def __new__(cls, item: Any, __item2: Any) -> itemgetter[tuple[Any, Any]]: ...
def __new__(cls, item: _T_co, __item2: _T_co) -> itemgetter[tuple[_T_co, _T_co]]: ...
@overload
def __new__(cls, item: Any, __item2: Any, __item3: Any) -> itemgetter[tuple[Any, Any, Any]]: ...
def __new__(cls, item: _T_co, __item2: _T_co, __item3: _T_co) -> itemgetter[tuple[_T_co, _T_co, _T_co]]: ...
@overload
def __new__(cls, item: Any, __item2: Any, __item3: Any, __item4: Any) -> itemgetter[tuple[Any, Any, Any, Any]]: ...
def __new__(
cls, item: _T_co, __item2: _T_co, __item3: _T_co, __item4: _T_co
) -> itemgetter[tuple[_T_co, _T_co, _T_co, _T_co]]: ...
@overload
def __new__(cls, item: Any, *items: Any) -> itemgetter[tuple[Any, ...]]: ...
def __call__(self, obj: Any) -> _T_co: ...
def __new__(
cls, item: _T_co, __item2: _T_co, __item3: _T_co, __item4: _T_co, *items: _T_co
) -> itemgetter[tuple[_T_co, ...]]: ...
# __key: _KT_contra in SupportsGetItem seems to be causing variance issues, ie:
# TypeVar "_KT_contra@SupportsGetItem" is contravariant
# "tuple[int, int]" is incompatible with protocol "SupportsIndex"
# preventing [_T_co, ...] instead of [Any, ...]
#
# 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: ...
@final
class methodcaller:

View File

@@ -119,7 +119,7 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
# stable
class SupportsGetItem(Protocol[_KT_contra, _VT_co]):
def __contains__(self, __x: object) -> bool: ...
def __contains__(self, __x: Any) -> bool: ...
def __getitem__(self, __key: _KT_contra) -> _VT_co: ...
# stable

View File

@@ -67,8 +67,11 @@ class mmap(Iterable[int], Sized):
def __setitem__(self, __index: int, __object: int) -> None: ...
@overload
def __setitem__(self, __index: slice, __object: ReadableBuffer) -> None: ...
# Doesn't actually exist, but the object is actually iterable because it has __getitem__ and
# __len__, so we claim that there is also an __iter__ to help type checkers.
# Doesn't actually exist, but the object actually supports "in" because it has __getitem__,
# so we claim that there is also a __contains__ to help type checkers.
def __contains__(self, __o: object) -> bool: ...
# Doesn't actually exist, but the object is actually iterable because it has __getitem__ and __len__,
# so we claim that there is also an __iter__ to help type checkers.
def __iter__(self) -> Iterator[int]: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, *args: object) -> None: ...