Skip to content

Commit dfcce0f

Browse files
committed
fix list.__add__ to use the expected type
1 parent bf484ab commit dfcce0f

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ 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+
l_int = [1, 2]
24+
l_str = ["a", "b"]
25+
_: List[object] = l_int + l_str
26+
_: List[None] = l_int + l_str # type: ignore
27+
28+
combined = l_int + l_str
29+
assert_type(combined, List[int | str])

stdlib/builtins.pyi

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,11 +1135,8 @@ class list(MutableSequence[_T]):
11351135
@overload
11361136
def __setitem__(self, key: slice, value: Iterable[_T], /) -> None: ...
11371137
def __delitem__(self, key: SupportsIndex | slice, /) -> None: ...
1138-
# Overloading looks unnecessary, but is needed to work around complex mypy problems
1139-
@overload
1140-
def __add__(self, value: list[_T], /) -> list[_T]: ...
1141-
@overload
1142-
def __add__(self, value: list[_S], /) -> list[_S | _T]: ...
1138+
# `__add__` returns a new object, so we capture the expected result type with a type variable
1139+
def __add__(self, value: list[_S], /) -> list[_T1 | _T | _S]: ...
11431140
def __iadd__(self, value: Iterable[_T], /) -> Self: ... # type: ignore[misc]
11441141
def __mul__(self, value: SupportsIndex, /) -> list[_T]: ...
11451142
def __rmul__(self, value: SupportsIndex, /) -> list[_T]: ...

0 commit comments

Comments
 (0)