Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions stdlib/_operator.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from builtins import _GetItemIterable
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use _typeshed.SupportsGetItem instead?

Copy link
Copy Markdown
Collaborator Author

@Avasam Avasam Nov 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot as-is, because SupportsGetItem defines __contains__, AND it is defined as def __contains__(self, __x: object) -> bool: ....
Not all indexable types (including buffer-like ones) define __contains__ *

bytes defines it as def __contains__(self, __o: SupportsIndex | bytes) -> bool: ..., and I am uncertain of the change required to make it work. (other than just typing the parameter __x as Any).

* On that note, mmap missing __contains__ seems to be a mistake:

>>> from mmap import mmap
>>> 0 in mmap(0, 5)     
False
>>> b'\x00' in mmap(0, 5)
True

I've done what I think are the necessary changes.

from collections.abc import Callable, Container, Iterable, Mapping, MutableMapping, MutableSequence, Sequence
from typing import Any, AnyStr, Generic, Protocol, SupportsAbs, TypeVar, overload
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, final
Expand Down Expand Up @@ -77,7 +78,7 @@ 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: ...
def getitem(__a: _GetItemIterable[_T], __b: SupportsIndex) -> _T: ...
@overload
def getitem(__a: Sequence[_T], __b: slice) -> Sequence[_T]: ...
@overload
Expand Down Expand Up @@ -107,16 +108,18 @@ class attrgetter(Generic[_T_co]):
@final
class itemgetter(Generic[_T_co]):
@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, *items: _T_co) -> itemgetter[tuple[_T_co, ...]]: ...
def __call__(self, obj: _GetItemIterable[_T_co]) -> _T_co: ...

@final
class methodcaller:
Expand Down
2 changes: 0 additions & 2 deletions stubs/six/@tests/stubtest_allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ six.create_bound_method.__defaults__
six.moves.*

# Implemented using "operator" functions in the implementation
six.byte2int
six.indexbytes
six.get_function_closure
six.get_function_code
six.get_function_defaults
Expand Down
9 changes: 6 additions & 3 deletions stubs/six/six/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import builtins
import operator
import types
import unittest
from _typeshed import IdentityFunction
Expand Down Expand Up @@ -63,9 +64,11 @@ def u(s: str) -> str: ...
unichr = chr

def int2byte(i: int) -> bytes: ...
def byte2int(bs: bytes) -> int: ...
def indexbytes(buf: bytes, i: int) -> int: ...
def iterbytes(buf: bytes) -> _Iterator[int]: ...

byte2int = operator.itemgetter(0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not legal in a stub.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, didn't catch that. I accidentally ran the tests locally with py instead of python (venv fail)

indexbytes = operator.getitem
iterbytes = iter

def assertCountEqual(self: unittest.TestCase, first: Iterable[_T], second: Iterable[_T], msg: str | None = ...) -> None: ...
@overload
def assertRaisesRegex(self: unittest.TestCase, msg: str | None = ...) -> Any: ...
Expand Down