@@ -1432,6 +1432,7 @@ reveal_type('bar' + d) # N: Revealed type is "Literal['barfoo']"
14321432
14331433reveal_type(a.__add__(b)) # N: Revealed type is "builtins.int"
14341434reveal_type(b.__add__(a)) # N: Revealed type is "builtins.int"
1435+ reveal_type(a.__add__(a)) # N: Revealed type is "builtins.int"
14351436
14361437a *= b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
14371438b *= a
@@ -2986,13 +2987,19 @@ class C(Base):
29862987[builtins fixtures/tuple.pyi]
29872988
29882989[case testLiteralAddition]
2989- from typing import Union
2990+ from typing import Any, Union
29902991from typing_extensions import Literal
29912992
2993+ class A:
2994+ def __add__(self, other: str) -> str: ...
2995+ def __radd__(self, other: str) -> str: ...
2996+
29922997str_a: Literal["a"]
29932998str_b: Literal["b"]
29942999str_union_1: Literal["a", "b"]
29953000str_union_2: Literal["d", "c"]
3001+ str_union_mixed_1: Union[Literal["a"], Any]
3002+ str_union_mixed_2: Union[Literal["a"], A]
29963003s: str
29973004int_1: Literal[1]
29983005int_2: Literal[2]
@@ -3020,6 +3027,10 @@ reveal_type(str_a + s) # N: Revealed type is "builtins.str"
30203027reveal_type(s + str_a) # N: Revealed type is "builtins.str"
30213028reveal_type(str_union_1 + s) # N: Revealed type is "builtins.str"
30223029reveal_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"
30233034
30243035reveal_type(1 + 2) # N: Revealed type is "builtins.int"
30253036reveal_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 "
30573068 # N: Revealed type is "builtins.str"
30583069[builtins fixtures/primitives.pyi]
30593070
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+
30603086[case testLiteralAdditionTypedDict]
30613087from typing import TypedDict
30623088from typing_extensions import Literal
0 commit comments