Skip to content

Tweak constraints handling for splitting typevartuples #13716

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
Sep 23, 2022

Conversation

jhance
Copy link
Collaborator

@jhance jhance commented Sep 23, 2022

The existing logic for splitting mapped & template into prefix, middle, and suffix does not handle the case where the template middle itself is not a singleton unpack but rather itself has a prefix & suffix.

In this case we need to pull out the prefix & suffix by doing a second round of splitting on the middle. Originally we weren't sure if the PEP required implementing this double split, but one of the PEP646 test cases requires it.

The existing logic for splitting mapped & template into prefix, middle,
and suffix does not handle the case where the template middle itself
is not a singleton unpack but rather itself has a prefix & suffix.

In this case we need to pull out the prefix & suffix by doing a second
round of splitting on the middle. Originally we weren't sure if the PEP
required implementing this double split, but one of the PEP646 test
cases requires it.
@github-actions
Copy link
Contributor

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

@JukkaL JukkaL changed the title Tweak constraints handling for splitting typevartuples. Tweak constraints handling for splitting typevartuples Sep 23, 2022
@JukkaL JukkaL merged commit 06aefc6 into python:master Sep 23, 2022
jhance added a commit to jhance/mypy that referenced this pull request Sep 23, 2022
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.
jhance added a commit that referenced this pull request Oct 13, 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.
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