Skip to content

Extend support for tuple subtyping with typevar tuples #13718

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 1 commit into from
Oct 13, 2022

Conversation

jhance
Copy link
Collaborator

@jhance jhance commented Sep 23, 2022

This adds a new Pep646 test case which demonstrates that like with the constraints from PR #13716 we need to split twice for subtyping when handling Unpacks.

It also demonstrates a weakness of the previous PR which is that the middle-prefix and the prefix may need to be handled differently so we introduce another splitting function that returns a 10-tuple instead of a 6-tuple and reimplement the 6-tuple version on top of the 10-tuple version.

Complicating things further, the test case reveals that there are error cases where split_with_mapped_and_template cannot actually unpack the middle a second time because the mapped middle is too short to do the unpack. We also now have to deal with the case where there was no unpack in the template in which case we only do a single split.

In addition we fix a behavioral issue where according to PEP646 we should assume that Tuple[Unpack[Tuple[Any, ...]]] is equivalent to Tuple[Any, Any] even if we don't actually know the lengths match. As such test_type_var_tuple_unpacked_variable_length_tuple changes from asserting a strict subtype to asserting equivalence.

One of the messages was bad as well so we add a branch for UnpackType in message pretty-printing.

This adds a new Pep646 test case which demonstrates that like with the
constraints from PR python#13716 we need to split twice for subtyping when
handling Unpacks.

It also demonstrates a weakness of the previous PR which is that the
middle-prefix and the prefix may need to be handled differently so
we introduce another splitting function that returns a 10-tuple instead
of a 6-tuple and reimplement the 6-tuple version on top of the 10-tuple
version.

Complicating things further, the test case reveals that there are error
cases where split_with_mapped_and_template cannot actually unpack the
middle a second time because the mapped middle is too short to do the
unpack. We also now have to deal with the case where there was no unpack
in the template in which case we only do a single split.

In addition we fix a behavioral issue where according to PEP646 we
should assume that Tuple[Unpack[Tuple[Any, ...]]] is equivalent to
Tuple[Any, Any] even if we don't actually know the lengths match. As
such test_type_var_tuple_unpacked_variable_length_tuple changes from
asserting a strict subtype to asserting equivalence.

One of the messages was bad as well so we add a branch for UnpackType
in message pretty-printing.
@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

LG

@jhance jhance merged commit 0d07c90 into python:master Oct 13, 2022
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.

2 participants