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

Fixed length tuple concatenation #7409

Merged
merged 5 commits into from Sep 2, 2019

Conversation

@onlined
Copy link
Contributor

commented Aug 29, 2019

Fixes #224.

@onlined onlined changed the title Add fixed length tuple concatenation Fixed length tuple concatenation Aug 29, 2019
Copy link
Collaborator

left a comment

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.

"""Concatenate two fixed length tuples."""
result = TupleType(items=left.items + right.items,
fallback=self.named_type('builtins.tuple'))
result.partial_fallback = tuple_fallback(result)

This comment has been minimized.

Copy link
@JukkaL

JukkaL Aug 29, 2019

Collaborator

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.

This comment has been minimized.

Copy link
@onlined

onlined Aug 30, 2019

Author Contributor

Thanks! Removed the line.

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)

This comment has been minimized.

Copy link
@JukkaL

JukkaL Aug 29, 2019

Collaborator

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.

This comment has been minimized.

Copy link
@onlined

onlined Aug 29, 2019

Author Contributor

Isn't the same thing applicable for __radd__ and right tuple?

This comment has been minimized.

Copy link
@JukkaL

JukkaL Aug 30, 2019

Collaborator

Yes, you are correct.

This comment has been minimized.

Copy link
@onlined

onlined Aug 30, 2019

Author Contributor

Added the check.

onlined added 2 commits Aug 30, 2019
@JukkaL

This comment has been minimized.

Copy link
Collaborator

commented Aug 30, 2019

To fix the build, you need to add tuple.__add__ to test-data/unit/fixtures/tuple.pyi.

@onlined

This comment has been minimized.

Copy link
Contributor Author

commented Aug 30, 2019

Thanks!

@onlined

This comment has been minimized.

Copy link
Contributor Author

commented Sep 1, 2019

@JukkaL The change in stubs broke a test, I think I should change the test too. What do you think?

@JukkaL

This comment has been minimized.

Copy link
Collaborator

commented Sep 2, 2019

Yeah, you should change the tests to match the new output.

@onlined

This comment has been minimized.

Copy link
Contributor Author

commented Sep 2, 2019

Fixed the tests!

@JukkaL
JukkaL approved these changes Sep 2, 2019
Copy link
Collaborator

left a comment

Thanks for the updates -- I'm glad tuple concatenation is finally supported!

@JukkaL JukkaL merged commit de6b291 into python:master Sep 2, 2019
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.