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

Fix joining of Sequence (e.g. variadic tuple) and fixed-length tuple #8335

Merged
merged 1 commit into from
Jan 31, 2020

Conversation

intgr
Copy link
Contributor

@intgr intgr commented Jan 26, 2020

For example:

  • Tuple[int] + Tuple[bool, ...] becomes Tuple[int, ...]
  • List[int] + Tuple[bool, ...] becomes Sequence[int]

This solves the other part of issue #4975 (along with PR #8333) and also issue #8074. Previously Mypy simply punted and returned object.

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2020

As discussed elsewhere, also testing subclasses of tuple would be a good idea.

@intgr intgr removed the request for review from msullivan January 29, 2020 23:45
@intgr intgr force-pushed the join-sequence-and-fixed-tuple branch from 5c552bb to 129cda7 Compare January 31, 2020 00:16
For example:
* Tuple[int] + Tuple[bool, ...] becomes Tuple[int, ...]
* List[int] + Tuple[bool, ...] becomes Sequence[int]

Previously Mypy simply punted and returned `object`.
@intgr intgr force-pushed the join-sequence-and-fixed-tuple branch from 129cda7 to 8bb19cf Compare January 31, 2020 00:46
@intgr
Copy link
Contributor Author

intgr commented Jan 31, 2020

I added test testTupleSubclassJoin, does that cover what you had in mind?

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, looks good! This is a nice type inference improvement.

@theodoretliu
Copy link
Contributor

Quick question: why is subclassing "automatically" handled by this PR? This is more just for my curiosity, does the "default fallback" function automatically find the ancestor of the class?

@intgr
Copy link
Contributor Author

intgr commented Feb 2, 2020

@theodoretliu The tuple_fallback function returns a variadic tuple in place of a fixed-length tuple. But most tuple subclasses only support fixed-length operation, it cannot construct NamedTuple[int, ...] so it has to fall back to builtin.tuple class for tuple subclasses.

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.

3 participants