@@ -1432,6 +1432,7 @@ reveal_type('bar' + d) # N: Revealed type is "Literal['barfoo']"
1432
1432
1433
1433
reveal_type(a.__add__(b)) # N: Revealed type is "builtins.int"
1434
1434
reveal_type(b.__add__(a)) # N: Revealed type is "builtins.int"
1435
+ reveal_type(a.__add__(a)) # N: Revealed type is "builtins.int"
1435
1436
1436
1437
a *= b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
1437
1438
b *= a
@@ -2986,13 +2987,19 @@ class C(Base):
2986
2987
[builtins fixtures/tuple.pyi]
2987
2988
2988
2989
[case testLiteralAddition]
2989
- from typing import Union
2990
+ from typing import Any, Union
2990
2991
from typing_extensions import Literal
2991
2992
2993
+ class A:
2994
+ def __add__(self, other: str) -> str: ...
2995
+ def __radd__(self, other: str) -> str: ...
2996
+
2992
2997
str_a: Literal["a"]
2993
2998
str_b: Literal["b"]
2994
2999
str_union_1: Literal["a", "b"]
2995
3000
str_union_2: Literal["d", "c"]
3001
+ str_union_mixed_1: Union[Literal["a"], Any]
3002
+ str_union_mixed_2: Union[Literal["a"], A]
2996
3003
s: str
2997
3004
int_1: Literal[1]
2998
3005
int_2: Literal[2]
@@ -3020,6 +3027,10 @@ reveal_type(str_a + s) # N: Revealed type is "builtins.str"
3020
3027
reveal_type(s + str_a) # N: Revealed type is "builtins.str"
3021
3028
reveal_type(str_union_1 + s) # N: Revealed type is "builtins.str"
3022
3029
reveal_type(s + str_union_1) # N: Revealed type is "builtins.str"
3030
+ reveal_type(str_a + str_union_mixed_1) # N: Revealed type is "builtins.str"
3031
+ reveal_type(str_union_mixed_1 + str_a) # N: Revealed type is "Union[builtins.str, Any]"
3032
+ reveal_type(str_a + str_union_mixed_2) # N: Revealed type is "builtins.str"
3033
+ reveal_type(str_union_mixed_2 + str_a) # N: Revealed type is "builtins.str"
3023
3034
3024
3035
reveal_type(1 + 2) # N: Revealed type is "builtins.int"
3025
3036
reveal_type(int_1 + int_2) # N: Revealed type is "Literal[3]"
@@ -3057,6 +3068,21 @@ reveal_type("a" + misc_union) # E: Unsupported operand types for + ("str" and "
3057
3068
# N: Revealed type is "builtins.str"
3058
3069
[builtins fixtures/primitives.pyi]
3059
3070
3071
+ [case testLiteralAdditionInheritance]
3072
+ class A:
3073
+ a = ""
3074
+
3075
+ class B(A):
3076
+ a = "a" + "b"
3077
+
3078
+ class C:
3079
+ a = "a" + "b"
3080
+
3081
+ reveal_type(A.a) # N: Revealed type is "builtins.str"
3082
+ reveal_type(B.a) # N: Revealed type is "builtins.str"
3083
+ reveal_type(C.a) # N: Revealed type is "builtins.str"
3084
+ [builtins fixtures/primitives.pyi]
3085
+
3060
3086
[case testLiteralAdditionTypedDict]
3061
3087
from typing import TypedDict
3062
3088
from typing_extensions import Literal
0 commit comments