@@ -1407,19 +1407,19 @@ c: Literal[4]
14071407d: Literal['foo']
14081408e: str
14091409
1410- reveal_type(a + a) # N: Revealed type is "builtins.int "
1410+ reveal_type(a + a) # N: Revealed type is "Literal[6] "
14111411reveal_type(a + b) # N: Revealed type is "builtins.int"
14121412reveal_type(b + a) # N: Revealed type is "builtins.int"
1413- reveal_type(a + 1) # N: Revealed type is "builtins.int "
1414- reveal_type(1 + a) # N: Revealed type is "builtins.int "
1415- reveal_type(a + c) # N: Revealed type is "builtins.int "
1416- reveal_type(c + a) # N: Revealed type is "builtins.int "
1413+ reveal_type(a + 1) # N: Revealed type is "Literal[4] "
1414+ reveal_type(1 + a) # N: Revealed type is "Literal[4] "
1415+ reveal_type(a + c) # N: Revealed type is "Literal[7] "
1416+ reveal_type(c + a) # N: Revealed type is "Literal[7] "
14171417
1418- reveal_type(d + d) # N: Revealed type is "builtins.str "
1418+ reveal_type(d + d) # N: Revealed type is "Literal['foofoo'] "
14191419reveal_type(d + e) # N: Revealed type is "builtins.str"
14201420reveal_type(e + d) # N: Revealed type is "builtins.str"
1421- reveal_type(d + 'foo ') # N: Revealed type is "builtins.str "
1422- reveal_type('foo ' + d) # N: Revealed type is "builtins.str "
1421+ reveal_type(d + 'bar ') # N: Revealed type is "Literal['foobar'] "
1422+ 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"
@@ -2976,3 +2976,87 @@ x: Type[Literal[1]] # E: Type[...] can't contain "Literal[...]"
29762976y: Type[Union[Literal[1], Literal[2]]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]"
29772977z: Type[Literal[1, 2]] # E: Type[...] can't contain "Union[Literal[...], Literal[...]]"
29782978[builtins fixtures/tuple.pyi]
2979+
2980+ [case testLiteralAddition]
2981+ from typing import Union
2982+ from typing_extensions import Literal
2983+
2984+ str_a: Literal["a"]
2985+ str_b: Literal["b"]
2986+ str_union_1: Literal["a", "b"]
2987+ str_union_2: Literal["c", "d"]
2988+ s: str
2989+ int_1: Literal[1]
2990+ int_2: Literal[2]
2991+ int_union_1: Literal[1, 2]
2992+ int_union_2: Literal[3, 4]
2993+ i: int
2994+ bytes_a: Literal[b"a"]
2995+ bytes_b: Literal[b"b"]
2996+ bytes_union_1: Literal[b"a", b"b"]
2997+ bytes_union_2: Literal[b"c", b"d"]
2998+ b: bytes
2999+
3000+ misc_union: Literal["a", 1]
3001+
3002+ reveal_type(str_a + str_b) # N: Revealed type is "Literal['ab']"
3003+ reveal_type(str_a + "b") # N: Revealed type is "Literal['ab']"
3004+ reveal_type("a" + str_b) # N: Revealed type is "Literal['ab']"
3005+ reveal_type(str_union_1 + "b") # N: Revealed type is "Union[Literal['ab'], Literal['bb']]"
3006+ reveal_type(str_union_1 + str_b) # N: Revealed type is "Union[Literal['ab'], Literal['bb']]"
3007+ reveal_type("a" + str_union_1) # N: Revealed type is "Union[Literal['aa'], Literal['ab']]"
3008+ reveal_type(str_a + str_union_1) # N: Revealed type is "Union[Literal['aa'], Literal['ab']]"
3009+ reveal_type(str_union_1 + str_union_2) # N: Revealed type is "Union[Literal['ac'], Literal['ad'], Literal['bc'], Literal['bd']]"
3010+ reveal_type(str_a + s) # N: Revealed type is "builtins.str"
3011+ reveal_type(s + str_a) # N: Revealed type is "builtins.str"
3012+ reveal_type(str_union_1 + s) # N: Revealed type is "builtins.str"
3013+ reveal_type(s + str_union_1) # N: Revealed type is "builtins.str"
3014+
3015+ reveal_type(int_1 + int_2) # N: Revealed type is "Literal[3]"
3016+ reveal_type(int_1 + 1) # N: Revealed type is "Literal[2]"
3017+ reveal_type(1 + int_1) # N: Revealed type is "Literal[2]"
3018+ reveal_type(int_union_1 + 1) # N: Revealed type is "Union[Literal[2], Literal[3]]"
3019+ reveal_type(int_union_1 + int_1) # N: Revealed type is "Union[Literal[2], Literal[3]]"
3020+ reveal_type(1 + int_union_1) # N: Revealed type is "Union[Literal[2], Literal[3]]"
3021+ reveal_type(int_1 + int_union_1) # N: Revealed type is "Union[Literal[2], Literal[3]]"
3022+ reveal_type(int_union_1 + int_union_2) # N: Revealed type is "Union[Literal[4], Literal[5], Literal[6]]"
3023+ reveal_type(int_1 + i) # N: Revealed type is "builtins.int"
3024+ reveal_type(i + int_1) # N: Revealed type is "builtins.int"
3025+ reveal_type(int_union_1 + i) # N: Revealed type is "builtins.int"
3026+ reveal_type(i + int_union_1) # N: Revealed type is "builtins.int"
3027+
3028+ reveal_type(bytes_a + bytes_b) # N: Revealed type is "Literal[b'ab']"
3029+ reveal_type(bytes_a + b"b") # N: Revealed type is "Literal[b'ab']"
3030+ reveal_type(b"a" + bytes_b) # N: Revealed type is "Literal[b'ab']"
3031+ reveal_type(bytes_union_1 + b"b") # N: Revealed type is "Union[Literal[b'ab'], Literal[b'bb']]"
3032+ reveal_type(bytes_union_1 + bytes_b) # N: Revealed type is "Union[Literal[b'ab'], Literal[b'bb']]"
3033+ reveal_type(b"a" + bytes_union_1) # N: Revealed type is "Union[Literal[b'aa'], Literal[b'ab']]"
3034+ reveal_type(bytes_a + bytes_union_1) # N: Revealed type is "Union[Literal[b'aa'], Literal[b'ab']]"
3035+ reveal_type(bytes_union_1 + bytes_union_2) # N: Revealed type is "Union[Literal[b'ac'], Literal[b'ad'], Literal[b'bc'], Literal[b'bd']]"
3036+ reveal_type(bytes_a + b) # N: Revealed type is "builtins.bytes"
3037+ reveal_type(b + bytes_a) # N: Revealed type is "builtins.bytes"
3038+ reveal_type(bytes_union_1 + b) # N: Revealed type is "builtins.bytes"
3039+ reveal_type(b + bytes_union_1) # N: Revealed type is "builtins.bytes"
3040+
3041+ reveal_type(misc_union + "a") # N: Revealed type is "Union[builtins.str, builtins.int]" \
3042+ # E: Unsupported operand types for + ("Literal[1]" and "str") \
3043+ # N: Left operand is of type "Literal['a', 1]"
3044+ reveal_type("a" + misc_union) # E: Unsupported operand types for + ("str" and "Literal[1]") \
3045+ # N: Right operand is of type "Literal['a', 1]" \
3046+ # N: Revealed type is "builtins.str"
3047+ [builtins fixtures/primitives.pyi]
3048+
3049+ [case testLiteralAdditionTypedDict]
3050+ from typing import TypedDict
3051+ from typing_extensions import Literal
3052+
3053+ class LookupDict(TypedDict):
3054+ top_var: str
3055+ bottom_var: str
3056+ var: str
3057+
3058+ def func(d: LookupDict, pos: Literal["top_", "bottom_", ""]) -> str:
3059+ return d[pos + "var"]
3060+
3061+ [builtins fixtures/dict.pyi]
3062+ [typing fixtures/typing-typeddict.pyi]
0 commit comments