Uh oh!
There was an error while loading. Please reload this page.
fix list concatenation - #2404
Conversation
ilevkivskyi
commented
Aug 19, 2018
I am not sure if this is the right solution. Maybe we can try something closer to I guess a perfect solution for this kind of questions would require python/mypy#2354 solved first. |
JukkaL
commented
Oct 12, 2018
The return type needs to use a union, similar to the set operations. I don't think that generic self types would help, since subclasses will default to generating classA(list): passtype(A([1]) +A([1])) islist# True |
ilevkivskyi
commented
Oct 12, 2018
Yes, indeed, it returns just a list. I remember people wanted generic self types when they want the return to be a subtype, but it is not the case here.
This actually might not solve the original issue, where a join is preferred: classB: ...
classC1(B): ...
classC2(B): ...
lst: List[B] = [C1()] + [C2()]This is however an eternal question of union vs join, so I think there is no ideal solution here. |
ilevkivskyi
commented
Oct 12, 2018
Just to clarify I do think returning a |
JelleZijlstra
commented
Oct 12, 2018
I am going to be out for another week and won't have time to update the code in this PR. If it's urgent, feel free to take over the PR and change the code as you see fit; otherwise I'll implement your suggestion in a few weeks. |
JelleZijlstra
commented
Oct 26, 2018
I implemented the Union return type suggestion now. |
The fix caused regressions for mypy that are difficult to fix. See python/mypy#5492 for context. This reverts commit 1a42a2c.
The fix caused regressions for mypy that are difficult to fix. See python/mypy#5492 for context. This reverts commit 1a42a2c.
The fix caused regressions for mypy that are difficult to fix. See python/mypy#5492 for context. This reverts commit 1a42a2c.
Fixes#2383, python/mypy#5492.
I was a little surprised this works, but it does. I tried the test cases from both of the linked issues, and mypy allows them without any errors when this typeshed patch is applied.
Unfortunately, with this change
__iadd__becomes incompatible with__add__according to mypy. I tried changing it todef __iadd__(self: List[_T_co], x: Iterable[_T_co]) -> List[_T_co]: ..., which passes mypy, but that breaks this test case:Therefore, I chose to
# type: ignoreit instead.