@@ -1423,6 +1423,7 @@ reveal_type('bar' + d) # N: Revealed type is "Literal['barfoo']"
14231423
14241424reveal_type(a.__add__(b)) # N: Revealed type is "builtins.int"
14251425reveal_type(b.__add__(a)) # N: Revealed type is "builtins.int"
1426+ reveal_type(a.__add__(a)) # N: Revealed type is "builtins.int"
14261427
14271428a *= b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
14281429b *= a
@@ -2978,13 +2979,19 @@ z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[...], Litera
29782979[builtins fixtures/tuple.pyi]
29792980
29802981[case testLiteralAddition]
2981- from typing import Union
2982+ from typing import Any, Union
29822983from typing_extensions import Literal
29832984
2985+ class A:
2986+ def __add__(self, other: str) -> str: ...
2987+ def __radd__(self, other: str) -> str: ...
2988+
29842989str_a: Literal["a"]
29852990str_b: Literal["b"]
29862991str_union_1: Literal["a", "b"]
29872992str_union_2: Literal["d", "c"]
2993+ str_union_mixed_1: Union[Literal["a"], Any]
2994+ str_union_mixed_2: Union[Literal["a"], A]
29882995s: str
29892996int_1: Literal[1]
29902997int_2: Literal[2]
@@ -3012,6 +3019,10 @@ reveal_type(str_a + s) # N: Revealed type is "builtins.str"
30123019reveal_type(s + str_a) # N: Revealed type is "builtins.str"
30133020reveal_type(str_union_1 + s) # N: Revealed type is "builtins.str"
30143021reveal_type(s + str_union_1) # N: Revealed type is "builtins.str"
3022+ reveal_type(str_a + str_union_mixed_1) # N: Revealed type is "builtins.str"
3023+ reveal_type(str_union_mixed_1 + str_a) # N: Revealed type is "Union[builtins.str, Any]"
3024+ reveal_type(str_a + str_union_mixed_2) # N: Revealed type is "builtins.str"
3025+ reveal_type(str_union_mixed_2 + str_a) # N: Revealed type is "builtins.str"
30153026
30163027reveal_type(1 + 2) # N: Revealed type is "builtins.int"
30173028reveal_type(int_1 + int_2) # N: Revealed type is "Literal[3]"
@@ -3049,6 +3060,21 @@ reveal_type("a" + misc_union) # E: Unsupported operand types for + ("str" and "
30493060 # N: Revealed type is "builtins.str"
30503061[builtins fixtures/primitives.pyi]
30513062
3063+ [case testLiteralAdditionInheritance]
3064+ class A:
3065+ a = ""
3066+
3067+ class B(A):
3068+ a = "a" + "b"
3069+
3070+ class C:
3071+ a = "a" + "b"
3072+
3073+ reveal_type(A.a) # N: Revealed type is "builtins.str"
3074+ reveal_type(B.a) # N: Revealed type is "builtins.str"
3075+ reveal_type(C.a) # N: Revealed type is "builtins.str"
3076+ [builtins fixtures/primitives.pyi]
3077+
30523078[case testLiteralAdditionTypedDict]
30533079from typing import TypedDict
30543080from typing_extensions import Literal
0 commit comments