Update comments for operator.itemgetter.__call__ generic following mypy 1.11 fix (#13489)

This commit is contained in:
Avasam
2025-10-08 04:51:14 -04:00
committed by GitHub
parent 751525bc74
commit cffeff0b61
4 changed files with 44 additions and 4 deletions
@@ -0,0 +1,38 @@
from __future__ import annotations
from _typeshed import SupportsDunderGT, SupportsDunderLT, SupportsGetItem
from collections.abc import Callable
from operator import itemgetter
from typing import Any, TypeVar
from typing_extensions import assert_type
_T = TypeVar("_T")
# This should be equivalent to itemgetter().__call__
def standalone_call(obj: SupportsGetItem[Any, _T]) -> _T: ...
# Expected type of itemgetter(1).__call__
expected_type_itemgetter_call: Callable[[SupportsGetItem[int, _T]], _T] # pyright: ignore[reportGeneralTypeIssues]
# Expecting itemgetter(1) to be assignable to this
# based on the example below: min({"first": 1, "second": 2}.items(), key=itemgetter(1))
# That example and assigning to this variable are what failed in https://github.com/python/mypy/issues/14032
expected_assignable_to: Callable[[tuple[str, int]], SupportsDunderLT[Any] | SupportsDunderGT[Any]]
# Regression tests for https://github.com/python/mypy/issues/14032
# assert_type(itemgetter("first")({"first": 1, "second": 2}), int) # See comment on itemgetter.__call__
assert_type(min({"first": 1, "second": 2}, key=itemgetter(1)), str)
assert_type(min({"first": 1, "second": 2}.items(), key=itemgetter(1)), tuple[str, int])
assert_type(standalone_call({"first": 1, "second": 2}), int)
assert_type(min({"first": 1, "second": 2}, key=standalone_call), str)
assert_type(min({"first": 1, "second": 2}.items(), key=standalone_call), tuple[str, int])
expected_itemgetter_call_type = itemgetter(1).__call__
expected_itemgetter_call_type = itemgetter(1)
expected_assignable_to = itemgetter(1)
expected_itemgetter_call_type = standalone_call
expected_assignable_to = standalone_call
+4 -2
View File
@@ -205,8 +205,10 @@ class itemgetter(Generic[_T_co]):
# "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
# If we can't infer a literal key from __new__ (ie: `itemgetter[Literal[0]]` for `itemgetter(0)`),
# then we can't annotate __call__'s return type or it'll break on tuples
#
# These issues are best demonstrated by the `itertools.check_itertools_recipes.unique_justseen` test.
def __call__(self, obj: SupportsGetItem[Any, Any]) -> Any: ...
@final
+1 -1
View File
@@ -14,7 +14,7 @@ six.get_method_self
six.viewitems
six.viewkeys
six.viewvalues
# Should be `operator.itemgetter[int]`. But a bug in mypy prevents using TypeVar in itemgetter__call__
# Should be `byte2int: operator.itemgetter[int]`. But `itemgetter.__call__` returns `Any`
six.byte2int
# Utils
+1 -1
View File
@@ -61,7 +61,7 @@ unichr = chr
def int2byte(i: int) -> bytes: ...
# Should be `byte2int: operator.itemgetter[int]`. But a bug in mypy prevents using TypeVar in itemgetter.__call__
# Should be `byte2int: operator.itemgetter[int]`. But `itemgetter.__call__` returns `Any`
def byte2int(obj: SupportsGetItem[int, _T]) -> _T: ...
indexbytes = operator.getitem