-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix list concatenation #2404
fix list concatenation #2404
Conversation
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. |
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 class A(list): pass
type(A([1]) + A([1])) is list # True |
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: class B: ...
class C1(B): ...
class C2(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. |
Just to clarify I do think returning a |
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. |
I implemented the Union return type suggestion now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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: ignore
it instead.