You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inferred Union subtype changed inconistently after a failed operation #3122
x: Union[int, str]
if isinstance(x, str):
reveal_type(x) # Revealed type is 'builtins.str'
x = x + 1 # E: Unsupported operand types for + ("str" and "int")
reveal_type(x) # Revealed type is 'builtins.int' - CHANGED
if isinstance(x, int):
reveal_type(x) # Revealed type is 'builtins.int'
x = x + 'a' # E: Unsupported operand types for + ("str" and "int")
reveal_type(x) # Revealed type is 'builtins.int' - DIDN'T CHANGE
I'm not sure whether CHANGE or NO CHANGE should be correct, but it should be consistent.
Related to this:
x: Union[int, str] = 'a'
x = x + 1 # error
reveal_type(x) # Revealed type: int
changed the title [-]Inferred Union subtype changed incorrectly after a failed operation[/-][+]Inferred Union subtype changed inconistently after a failed operation[/+]on Apr 3, 2017
fromtypingimportUnionx: Union[int, str]
ifisinstance(x, str):
reveal_type(x) # Revealed type is 'builtins.str'x=x+1# E: Unsupported operand types for + ("str" and "int")reveal_type(x) # Revealed type is 'Union[builtins.int, builtins.str]' - CHANGEDifisinstance(x, int):
reveal_type(x) # Revealed type is 'builtins.int'x=x+'a'# E: Unsupported operand types for + ("str" and "int")reveal_type(x) # Revealed type is 'builtins.int' - DIDN'T CHANGE
Possibly related: int.__radd__ exists in typeshed, but str.__radd__ doesn't. This matches reality:
>>> str.__radd__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'str' has no attribute '__radd__'. Did you mean: '__add__'?
>>> int.__radd__
<slot wrapper '__radd__' of 'int' objects>
Activity
[-]Inferred Union subtype changed incorrectly after a failed operation[/-][+]Inferred Union subtype changed inconistently after a failed operation[/+]JukkaL commentedon Apr 4, 2017
This boils down to the inferred types of these expressions:
This seems inconsistent.
AlexWaygood commentedon Mar 24, 2022
The output on mypy 0.941 is slightly different:
And for Jukka's example:
JelleZijlstra commentedon Mar 24, 2022
Possibly related:
int.__radd__exists in typeshed, butstr.__radd__doesn't. This matches reality: