-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type checking fails to distribute across Tuple types #18922
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
Comments
Is this a bug that need a fix? If so, can I work on this? |
Yes this is a bug that needs a fix, but it probably touches some decently complicated subtyping rules so it's probably not the best first contribution :^) Feel free to work on it though. |
To follow up on this, this is actually in the typing spec! See https://typing.python.org/en/latest/spec/tuples.html#type-compatibility-rules |
Thank you for proving more information! |
Hi, I tried working on this bug and I think I successful catch the example mention in the bug report, but my implementation leads to a maximum recursive depth error for recursive data types. Can anyone take a look and see whether this implementation is on the correct idea or is totally off? I didn't want to PR to the mypy repo broken code, so I just provided a link to a PR in my fork repo with the current issue: VallinZ#4 |
I think eagerly expanding tuples of unions as you do is probably a bad idea. I haven't thought too much about it (and my first instinct was IMO wrong), but I think you could add a special case next to the current exhaustive enum/bool one which:
(I haven't really played through this in my head so I can't say whether that's totally correct logic or not. Hopefully it at least gives an idea!) However, when I asked a few days ago people recommended that maybe normalizing (like you did) is a good idea, so... well, I don't know the specific reason you're running into issues with recursive types either. |
Nevermind that approach is wrong. Sorry, I don't know if there's a better way to implement this than what you have. (maybe avoiding creating all the tuples up front is possible? Left isn't in right and left is a tuple and right is a union, then you could split left on the first union element it has -- so that e.g. |
Got it, thank you for providing feedback! I'll look into the approach you suggested. |
Bug Report
Union
types fail to distribute acrossTuple
types as they ought to when the set of concrete values of each type are identical.To Reproduce
(https://mypy-play.net/?mypy=latest&python=3.12&gist=9d0052b0ed30856e5ad3985237f8754b)
Expected Behavior
Values of type
Tuple[float, Optional[float]]
andUnion[Tuple[float, float], Tuple[float, None]]
should be mutually compatible.That is:
Union
ought to distribute acrossTuple
.Actual Behavior
Your Environment
(See Playground link.)
The text was updated successfully, but these errors were encountered: