Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yet another attempt to fix list.__add__ #8293

Merged
merged 12 commits into from
Jul 15, 2022
6 changes: 5 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,12 @@ class list(MutableSequence[_T], Generic[_T]):
@overload
def __setitem__(self, __s: slice, __o: Iterable[_T]) -> None: ...
def __delitem__(self, __i: SupportsIndex | slice) -> None: ...
# Overloading looks unnecessary, but is needed to work around complex mypy problems
@overload
def __add__(self, __x: list[_T]) -> list[_T]: ...
def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ...
@overload
def __add__(self, __x: list[_S]) -> list[_S | _T]: ...
def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... # type: ignore[misc]
def __mul__(self, __n: SupportsIndex) -> list[_T]: ...
def __rmul__(self, __n: SupportsIndex) -> list[_T]: ...
def __imul__(self: Self, __n: SupportsIndex) -> Self: ...
Expand Down
16 changes: 16 additions & 0 deletions test_cases/stdlib/builtins/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing_extensions import assert_type


# list.__add__ example from #8292
class Foo:
def asd(self) -> int:
return 1


class Bar:
def asd(self) -> int:
return 2


combined = [Foo()] + [Bar()]
assert_type(combined, list[Foo | Bar])
Akuli marked this conversation as resolved.
Show resolved Hide resolved