-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fixed length tuple concatenation #7409
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
Conversation
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.
Thanks, this feature has been requested many times! I left one minor note and found an edge case that would be nice to handle. Otherwise looks solid.
mypy/checkexpr.py
Outdated
"""Concatenate two fixed length tuples.""" | ||
result = TupleType(items=left.items + right.items, | ||
fallback=self.named_type('builtins.tuple')) | ||
result.partial_fallback = tuple_fallback(result) |
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.
This line seems unnecessary. Anybody who needs the fallback type should just call tuple_fallback
instead of assuming that partial_fallback
has the correct type.
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.
Thanks! Removed the line.
mypy/checkexpr.py
Outdated
if isinstance(proper_left_type, TupleType) and e.op == '+': | ||
proper_right_type = get_proper_type(self.accept(e.right)) | ||
if isinstance(proper_right_type, TupleType): | ||
return self.concat_tuples(proper_left_type, proper_right_type) |
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.
There is a slight complication here: it's possible that the fallback type has a custom __add__
override that does something different from what's defined in tuple
. In that case we can't safely use this special casing. So there should be a check that checks whether the __add__
method in the left partial fallback comes from builtins.tuple
. If not, we use the old code path below instead of using concat_tuples
. Let me know if you need more hints about how to do this.
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.
Isn't the same thing applicable for __radd__
and right tuple?
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.
Yes, you are correct.
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.
Added the check.
To fix the build, you need to add |
Thanks! |
@JukkaL The change in stubs broke a test, I think I should change the test too. What do you think? |
Yeah, you should change the tests to match the new output. |
Fixed the tests! |
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.
Thanks for the updates -- I'm glad tuple concatenation is finally supported!
Fixes #224.