Skip to content

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

Merged
merged 5 commits into from
Sep 2, 2019

Conversation

onlined
Copy link
Contributor

@onlined onlined 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

@JukkaL JukkaL left a 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.

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the check.

@JukkaL
Copy link
Collaborator

JukkaL commented Aug 30, 2019

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

@onlined
Copy link
Contributor Author

onlined commented Aug 30, 2019

Thanks!

@onlined
Copy link
Contributor Author

onlined 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
Copy link
Collaborator

JukkaL commented Sep 2, 2019

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

@onlined
Copy link
Contributor Author

onlined commented Sep 2, 2019

Fixed the tests!

Copy link
Collaborator

@JukkaL JukkaL left a 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!

@JukkaL JukkaL merged commit de6b291 into python:master Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Concatenation of tuples
2 participants