From 0f25caed2d26d397e425073a4a9dcf8f316e5472 Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Sat, 28 Feb 2026 18:48:00 +0100 Subject: [PATCH] Update list.{count, index, remove} to accept `object` type --- stdlib/@tests/test_cases/builtins/check_list.py | 9 +++++++++ stdlib/builtins.pyi | 6 +++--- stubs/boltons/boltons/listutils.pyi | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/stdlib/@tests/test_cases/builtins/check_list.py b/stdlib/@tests/test_cases/builtins/check_list.py index 4113f5c66182..bf83c0730633 100644 --- a/stdlib/@tests/test_cases/builtins/check_list.py +++ b/stdlib/@tests/test_cases/builtins/check_list.py @@ -19,3 +19,12 @@ def asd(self) -> int: assert_type(combined, List[Union[Foo, Bar]]) for item in combined: assert_type(item.asd(), int) + + +def test_list_signature_overlapping_type(l: list[int], key: float) -> None: + # list.{index, count, remove} allow any type for the key since + # (a) the key may overlap with the list's element type, and + # (b) these methods are equality-based anyway. + l.index(key) + l.count(key) + l.remove(key) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 5695b17ca36d..48979d292bee 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1128,10 +1128,10 @@ class list(MutableSequence[_T]): def pop(self, index: SupportsIndex = -1, /) -> _T: ... # Signature of `list.index` should be kept in line with `collections.UserList.index()` # and multiprocessing.managers.ListProxy.index() - def index(self, value: _T, start: SupportsIndex = 0, stop: SupportsIndex = sys.maxsize, /) -> int: ... - def count(self, value: _T, /) -> int: ... + def index(self, value: object, start: SupportsIndex = 0, stop: SupportsIndex = sys.maxsize, /) -> int: ... + def count(self, value: object, /) -> int: ... def insert(self, index: SupportsIndex, object: _T, /) -> None: ... - def remove(self, value: _T, /) -> None: ... + def remove(self, value: object, /) -> None: ... # Signature of `list.sort` should be kept inline with `collections.UserList.sort()` # and multiprocessing.managers.ListProxy.sort() # diff --git a/stubs/boltons/boltons/listutils.pyi b/stubs/boltons/boltons/listutils.pyi index 35b8332ff14f..3bf3ad8eaf3a 100644 --- a/stubs/boltons/boltons/listutils.pyi +++ b/stubs/boltons/boltons/listutils.pyi @@ -20,8 +20,8 @@ class BarrelList(list[_T]): def __setslice__(self, start: SupportsIndex, stop: SupportsIndex, sequence: Iterable[_T]) -> None: ... def sort(self) -> None: ... # type: ignore[override] def reverse(self) -> None: ... - def count(self, item: _T) -> int: ... - def index(self, item: _T) -> int: ... # type: ignore[override] + def count(self, item: object) -> int: ... + def index(self, item: object) -> int: ... # type: ignore[override] BList: TypeAlias = BarrelList[_T]