From dfa7f7cfbdbdd688edaba0b9a408f7329adc0d27 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 00:52:57 +0300 Subject: [PATCH 01/12] naive attempt --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 173ecb176d20..25aca7f9f33e 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -986,7 +986,7 @@ class list(MutableSequence[_T], Generic[_T]): @overload def __setitem__(self, __s: slice, __o: Iterable[_T]) -> None: ... def __delitem__(self, __i: SupportsIndex | slice) -> None: ... - def __add__(self, __x: list[_T]) -> list[_T]: ... + def __add__(self, __x: list[_S]) -> list[_S | _T]: ... def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... def __mul__(self, __n: SupportsIndex) -> list[_T]: ... def __rmul__(self, __n: SupportsIndex) -> list[_T]: ... From 57dba4aed7514c9d3794e4eb1508e959df506b65 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 00:54:41 +0300 Subject: [PATCH 02/12] ignore --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 25aca7f9f33e..c1a918f93d5d 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -987,7 +987,7 @@ class list(MutableSequence[_T], Generic[_T]): def __setitem__(self, __s: slice, __o: Iterable[_T]) -> None: ... def __delitem__(self, __i: SupportsIndex | slice) -> None: ... def __add__(self, __x: list[_S]) -> list[_S | _T]: ... - def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... + def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... # type: ignore def __mul__(self, __n: SupportsIndex) -> list[_T]: ... def __rmul__(self, __n: SupportsIndex) -> list[_T]: ... def __imul__(self: Self, __n: SupportsIndex) -> Self: ... From ad88f6559eef9ba26910893147d0055e99553154 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:03:52 +0300 Subject: [PATCH 03/12] overload --- stdlib/builtins.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index c1a918f93d5d..36bde2dd4101 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -986,6 +986,9 @@ class list(MutableSequence[_T], Generic[_T]): @overload def __setitem__(self, __s: slice, __o: Iterable[_T]) -> None: ... def __delitem__(self, __i: SupportsIndex | slice) -> None: ... + @overload + def __add__(self, __x: list[_T]) -> list[_T]: ... + @overload def __add__(self, __x: list[_S]) -> list[_S | _T]: ... def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... # type: ignore def __mul__(self, __n: SupportsIndex) -> list[_T]: ... From de75e917138a9f6253ed9ae42bf042003c6087bb Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:14:59 +0300 Subject: [PATCH 04/12] add comment --- stdlib/builtins.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 36bde2dd4101..c347388e89f6 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -986,6 +986,7 @@ 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]: ... @overload From 8d9e7360c6e81366831f427cff862026bcfdaa24 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:32:13 +0300 Subject: [PATCH 05/12] add test case --- test_cases/stdlib/builtins/test_list.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test_cases/stdlib/builtins/test_list.py diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py new file mode 100644 index 000000000000..e8489b1e4615 --- /dev/null +++ b/test_cases/stdlib/builtins/test_list.py @@ -0,0 +1,13 @@ +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, Foo | Bar) From eb2e48cc35e670ca376fc0aef006d059f51e0f3b Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:32:36 +0300 Subject: [PATCH 06/12] Update stdlib/builtins.pyi Co-authored-by: Alex Waygood --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index c347388e89f6..525fe51465e1 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -991,7 +991,7 @@ class list(MutableSequence[_T], Generic[_T]): def __add__(self, __x: list[_T]) -> list[_T]: ... @overload def __add__(self, __x: list[_S]) -> list[_S | _T]: ... - def __iadd__(self: Self, __x: Iterable[_T]) -> Self: ... # type: ignore + 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: ... From 292d4e28a5108704c6b75903344c50312788ef7e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Jul 2022 22:32:49 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks --- test_cases/stdlib/builtins/test_list.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py index e8489b1e4615..7ce9af7976e6 100644 --- a/test_cases/stdlib/builtins/test_list.py +++ b/test_cases/stdlib/builtins/test_list.py @@ -1,13 +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, Foo | Bar) From c74e86c810f7395ba6ea80da9024aec30155c5ba Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:35:26 +0300 Subject: [PATCH 08/12] fix test --- test_cases/stdlib/builtins/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py index 7ce9af7976e6..4668c3427ed4 100644 --- a/test_cases/stdlib/builtins/test_list.py +++ b/test_cases/stdlib/builtins/test_list.py @@ -13,4 +13,4 @@ def asd(self) -> int: combined = [Foo()] + [Bar()] -assert_type(combined, Foo | Bar) +assert_type(combined, list[Foo | Bar]) From 5a6ae8275704445d6f78960411a92a0ade01fa67 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:38:16 +0300 Subject: [PATCH 09/12] Update test_cases/stdlib/builtins/test_list.py Co-authored-by: Alex Waygood --- test_cases/stdlib/builtins/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py index 4668c3427ed4..6be938f973c4 100644 --- a/test_cases/stdlib/builtins/test_list.py +++ b/test_cases/stdlib/builtins/test_list.py @@ -13,4 +13,4 @@ def asd(self) -> int: combined = [Foo()] + [Bar()] -assert_type(combined, list[Foo | Bar]) +assert_type(combined, List[Union[Foo, Bar]]) From 977b903f9dd00a4f678539afd9b221d21650e4ff Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 01:39:47 +0300 Subject: [PATCH 10/12] actually fixening the test fix --- test_cases/stdlib/builtins/test_list.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py index 6be938f973c4..43be9985c7ce 100644 --- a/test_cases/stdlib/builtins/test_list.py +++ b/test_cases/stdlib/builtins/test_list.py @@ -1,3 +1,4 @@ +from typing import List, Union from typing_extensions import assert_type From 612b39e25ed9f005b991d9b4fee67d6905caf232 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 21:19:34 +0300 Subject: [PATCH 11/12] add a bit more to test --- test_cases/stdlib/builtins/test_list.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py index 43be9985c7ce..0e6c7bfae9c3 100644 --- a/test_cases/stdlib/builtins/test_list.py +++ b/test_cases/stdlib/builtins/test_list.py @@ -15,3 +15,5 @@ def asd(self) -> int: combined = [Foo()] + [Bar()] assert_type(combined, List[Union[Foo, Bar]]) +for item in combined: + assert_type(item, int) From 5a81698bdef7c7e7b43cecc6c6ed30f6d01f0460 Mon Sep 17 00:00:00 2001 From: Akuli Date: Thu, 14 Jul 2022 21:29:39 +0300 Subject: [PATCH 12/12] fix the test case once again --- test_cases/stdlib/builtins/test_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/builtins/test_list.py b/test_cases/stdlib/builtins/test_list.py index 0e6c7bfae9c3..793d7951bee1 100644 --- a/test_cases/stdlib/builtins/test_list.py +++ b/test_cases/stdlib/builtins/test_list.py @@ -16,4 +16,4 @@ def asd(self) -> int: combined = [Foo()] + [Bar()] assert_type(combined, List[Union[Foo, Bar]]) for item in combined: - assert_type(item, int) + assert_type(item.asd(), int)