Skip to content

Commit 4ac3aa3

Browse files
committed
fix list.copy to use the expected type
1 parent bf484ab commit 4ac3aa3

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ 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+
l1: list[int] = [1]
24+
l2: list[object] = l1.copy()
25+
# this is an error, because a list of ints can't be a list of strs
26+
l3: list[str] = l1.copy() # type: ignore

stdlib/builtins.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,8 @@ class list(MutableSequence[_T]):
11041104
def __init__(self) -> None: ...
11051105
@overload
11061106
def __init__(self, iterable: Iterable[_T], /) -> None: ...
1107-
def copy(self) -> list[_T]: ...
1107+
# `copy` returns a new object, so capture the expected return type here using a type var
1108+
def copy(self) -> list[_S | _T]: ...
11081109
def append(self, object: _T, /) -> None: ...
11091110
def extend(self, iterable: Iterable[_T], /) -> None: ...
11101111
def pop(self, index: SupportsIndex = -1, /) -> _T: ...

0 commit comments

Comments
 (0)