-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 make_simplified_union interaction with Any #2714
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,19 @@ def meet_types(s: Type, t: Type) -> Type: | |
return t.accept(TypeMeetVisitor(s)) | ||
|
||
|
||
def meet_simple(s: Type, t: Type, default_right: bool = True) -> Type: | ||
def meet_simple(s: Type, t: Type) -> Type: | ||
"""Return the type s "narrowed down" to t. | ||
|
||
Note that this is not symmetric with respect to s and t. | ||
|
||
TODO: Explain what this does in more detail and how this is | ||
different from meet_types. | ||
""" | ||
if s == t: | ||
return s | ||
if isinstance(t, AnyType): | ||
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. Why this change? What effect does it have overall? 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. Without this, |
||
# Anything can be narrowed down to Any. | ||
return t | ||
if isinstance(s, UnionType): | ||
return UnionType.make_simplified_union([meet_types(x, t) for x in s.items]) | ||
elif not is_overlapping_types(s, t, use_promotions=True): | ||
|
@@ -36,10 +46,7 @@ def meet_simple(s: Type, t: Type, default_right: bool = True) -> Type: | |
else: | ||
return NoneTyp() | ||
else: | ||
if default_right: | ||
return t | ||
else: | ||
return s | ||
return t | ||
|
||
|
||
def is_overlapping_types(t: Type, s: Type, use_promotions: bool = False) -> bool: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this not a problem before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a regression in
testUnionAndBinaryOperation
(and another test case whose name I can't remember right now). Previously, if some of the results wasAny
, the result got simplified intoAny
. Without this change, we'd preserve the non-Any
types and this would sometimes produce a duplicate, similar-looking error message about the same binary operation, which would be a regression. Now the code falls back to the old behavior in these cases, and since this only happens when we have already generated a message, it doesn't affect correctness, just the specific messages generated for type errors involving union types.