Fix make_simplified_union for instances with last_known_value #10373
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This fixes
make_simplified_union
if multiple instances of the same type but with differentlast_known_value
s are passed to it.Previously it would falsely keep the
last_known_value
of the first occurrence:I'm not sure if this can currently actually occur at runtime, but I ran into this problem while developing #10191 and decided to submit it as a separate PR, as it's not directly related to #10191.
More specifically I need it for match statements such as
Note that I need to use instances with last known value and can't use LiteralType as that would prevent changing the value of
num
.With this change the
last_known_value
is taken into account before a type is removed from the union.Test Plan
mypy current doesn't have tests for
make_simplified_union
, but the behavior is implicitly tested by other code using it.This change causes a minor regression in one test, however that test was only working by pure chance before, as it used the fact that
make_simplified_union([Literal[3]?, int])
returnedLiteral[3]?
, which is obviously incorrect.