Skip to content

Commit d14b697

Browse files
committed
Add additional test cases
1 parent 12b518e commit d14b697

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

test-data/unit/check-literal.test

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,7 @@ reveal_type('bar' + d) # N: Revealed type is "Literal['barfoo']"
14231423

14241424
reveal_type(a.__add__(b)) # N: Revealed type is "builtins.int"
14251425
reveal_type(b.__add__(a)) # N: Revealed type is "builtins.int"
1426+
reveal_type(a.__add__(a)) # N: Revealed type is "builtins.int"
14261427

14271428
a *= b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
14281429
b *= 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
29822983
from typing_extensions import Literal
29832984

2985+
class A:
2986+
def __add__(self, other: str) -> str: ...
2987+
def __radd__(self, other: str) -> str: ...
2988+
29842989
str_a: Literal["a"]
29852990
str_b: Literal["b"]
29862991
str_union_1: Literal["a", "b"]
29872992
str_union_2: Literal["d", "c"]
2993+
str_union_mixed_1: Union[Literal["a"], Any]
2994+
str_union_mixed_2: Union[Literal["a"], A]
29882995
s: str
29892996
int_1: Literal[1]
29902997
int_2: Literal[2]
@@ -3012,6 +3019,10 @@ reveal_type(str_a + s) # N: Revealed type is "builtins.str"
30123019
reveal_type(s + str_a) # N: Revealed type is "builtins.str"
30133020
reveal_type(str_union_1 + s) # N: Revealed type is "builtins.str"
30143021
reveal_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

30163027
reveal_type(1 + 2) # N: Revealed type is "builtins.int"
30173028
reveal_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]
30533079
from typing import TypedDict
30543080
from typing_extensions import Literal

0 commit comments

Comments
 (0)