|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from _typeshed import SupportsDunderGT, SupportsDunderLT, SupportsGetItem |
| 4 | +from collections.abc import Callable |
| 5 | +from operator import itemgetter |
| 6 | +from typing import Any, TypeVar |
| 7 | +from typing_extensions import assert_type |
| 8 | + |
| 9 | +_T = TypeVar("_T") |
| 10 | + |
| 11 | + |
| 12 | +# This should be equivalent to itemgetter().__call__ |
| 13 | +def standalone_call(obj: SupportsGetItem[Any, _T]) -> _T: ... |
| 14 | + |
| 15 | + |
| 16 | +# Expected type of itemgetter(1).__call__ |
| 17 | +expected_type_itemgetter_call: Callable[[SupportsGetItem[int, _T]], _T] # pyright: ignore[reportGeneralTypeIssues] |
| 18 | + |
| 19 | +# Expecting itemgetter(1) to be assignable to this |
| 20 | +# based on the example below: min({"first": 1, "second": 2}.items(), key=itemgetter(1)) |
| 21 | +# That example and assigning to this variable are what failed in https://github.com/python/mypy/issues/14032 |
| 22 | +expected_assignable_to: Callable[[tuple[str, int]], SupportsDunderLT[Any] | SupportsDunderGT[Any]] |
| 23 | + |
| 24 | + |
| 25 | +# Regression tests for https://github.com/python/mypy/issues/14032 |
| 26 | +# assert_type(itemgetter("first")({"first": 1, "second": 2}), int) # See comment on itemgetter.__call__ |
| 27 | +assert_type(min({"first": 1, "second": 2}, key=itemgetter(1)), str) |
| 28 | +assert_type(min({"first": 1, "second": 2}.items(), key=itemgetter(1)), tuple[str, int]) |
| 29 | +assert_type(standalone_call({"first": 1, "second": 2}), int) |
| 30 | +assert_type(min({"first": 1, "second": 2}, key=standalone_call), str) |
| 31 | +assert_type(min({"first": 1, "second": 2}.items(), key=standalone_call), tuple[str, int]) |
| 32 | + |
| 33 | +expected_itemgetter_call_type = itemgetter(1).__call__ |
| 34 | +expected_itemgetter_call_type = itemgetter(1) |
| 35 | +expected_assignable_to = itemgetter(1) |
| 36 | + |
| 37 | +expected_itemgetter_call_type = standalone_call |
| 38 | +expected_assignable_to = standalone_call |
0 commit comments