-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Use (simplified) unions instead of joins for tuple fallbacks #17408
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
Changes from 1 commit
66ce501
59ab31c
8d69bc1
8b8b543
18c921e
a32b49a
23a5d03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1289,3 +1289,58 @@ x: str = a_class_or_none.field | |
a_or_none: Optional[A] | ||
y: int = a_or_none.field | ||
[builtins fixtures/list.pyi] | ||
|
||
[case testLargeUnionsShort] | ||
from typing import Union | ||
|
||
class C1: ... | ||
class C2: ... | ||
class C3: ... | ||
class C4: ... | ||
class C5: ... | ||
class C6: ... | ||
class C7: ... | ||
class C8: ... | ||
class C9: ... | ||
class C10: ... | ||
class C11: ... | ||
|
||
u: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11] | ||
x: int = u # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, <6 more items>]", variable has type "int") | ||
|
||
[case testLargeUnionsLongIfNeeded] | ||
from typing import Union | ||
|
||
class C1: ... | ||
class C2: ... | ||
class C3: ... | ||
class C4: ... | ||
class C5: ... | ||
class C6: ... | ||
class C7: ... | ||
class C8: ... | ||
class C9: ... | ||
class C10: ... | ||
|
||
x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int] | ||
y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str] | ||
x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str]", variable has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int]") | ||
|
||
[case testLargeUnionsNoneShown] | ||
from typing import Union | ||
|
||
class C1: ... | ||
class C2: ... | ||
class C3: ... | ||
class C4: ... | ||
class C5: ... | ||
class C6: ... | ||
class C7: ... | ||
class C8: ... | ||
class C9: ... | ||
class C10: ... | ||
class C11: ... | ||
|
||
x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11] | ||
y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, None] | ||
x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, <6 more items>, None]", variable has type "Union[C1, C2, C3, C4, C5, <6 more items>]") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I like this idea. I think I'd prefer to see the exact type mypy has inferred in an error message, even if it means the message is very verbose. I'd much prefer to have all the information I might need to debug the issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For debugging you can still use some Actually now that I am writing this, one thing that may be worth considering is some kind of stable sort for union items and/or flattening nesting unions (we already flatten most unions, but not those that are targets of type aliases). I will play with this locally now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, actually it looks worse when sorted. It seems more natural to see the same order as in the definitions. I would propose to just move on. I will be happy to revert this, if people will actually start complaining. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I agree having them in the order given in the source code is definitely better. I can remember times when omitting some overloads in the error message given to the user has made some issues much harder for me to debug over at typeshed, so I'm still not sure about abbreviating the union here. But I can see that it's consistent with what mypy does in other areas, so I won't die on this hill :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One last thing that I just tried locally and seems good is to add a specific note for long unions about which subtype item(s) cause problem. |
Uh oh!
There was an error while loading. Please reload this page.