Skip to content

Commit 0f25cae

Browse files
Update list.{count, index, remove} to accept object type
1 parent 843c1fd commit 0f25cae

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

stdlib/@tests/test_cases/builtins/check_list.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ def asd(self) -> int:
1919
assert_type(combined, List[Union[Foo, Bar]])
2020
for item in combined:
2121
assert_type(item.asd(), int)
22+
23+
24+
def test_list_signature_overlapping_type(l: list[int], key: float) -> None:
25+
# list.{index, count, remove} allow any type for the key since
26+
# (a) the key may overlap with the list's element type, and
27+
# (b) these methods are equality-based anyway.
28+
l.index(key)
29+
l.count(key)
30+
l.remove(key)

stdlib/builtins.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,10 @@ class list(MutableSequence[_T]):
11281128
def pop(self, index: SupportsIndex = -1, /) -> _T: ...
11291129
# Signature of `list.index` should be kept in line with `collections.UserList.index()`
11301130
# and multiprocessing.managers.ListProxy.index()
1131-
def index(self, value: _T, start: SupportsIndex = 0, stop: SupportsIndex = sys.maxsize, /) -> int: ...
1132-
def count(self, value: _T, /) -> int: ...
1131+
def index(self, value: object, start: SupportsIndex = 0, stop: SupportsIndex = sys.maxsize, /) -> int: ...
1132+
def count(self, value: object, /) -> int: ...
11331133
def insert(self, index: SupportsIndex, object: _T, /) -> None: ...
1134-
def remove(self, value: _T, /) -> None: ...
1134+
def remove(self, value: object, /) -> None: ...
11351135
# Signature of `list.sort` should be kept inline with `collections.UserList.sort()`
11361136
# and multiprocessing.managers.ListProxy.sort()
11371137
#

stubs/boltons/boltons/listutils.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class BarrelList(list[_T]):
2020
def __setslice__(self, start: SupportsIndex, stop: SupportsIndex, sequence: Iterable[_T]) -> None: ...
2121
def sort(self) -> None: ... # type: ignore[override]
2222
def reverse(self) -> None: ...
23-
def count(self, item: _T) -> int: ...
24-
def index(self, item: _T) -> int: ... # type: ignore[override]
23+
def count(self, item: object) -> int: ...
24+
def index(self, item: object) -> int: ... # type: ignore[override]
2525

2626
BList: TypeAlias = BarrelList[_T]
2727

0 commit comments

Comments
 (0)