You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# IntEnum literals should be accepted where int literals are expected
3022
+
takes_int_literal_1(MyIntEnum.ONE) # OK
3023
+
takes_int_literal_1_2_3(MyIntEnum.TWO) # OK
3024
+
3025
+
# Custom (int, Enum) literals should also be accepted
3026
+
takes_int_literal_1(MyCustomIntEnum.ONE) # OK
3027
+
takes_int_literal_1_2_3(MyCustomIntEnum.TWO) # OK
3028
+
3029
+
# Regular Enum literals should not be accepted
3030
+
takes_int_literal_1(MyEnum.ONE) # E: Argument 1 to "takes_int_literal_1" has incompatible type "Literal[MyEnum.ONE]"; expected "Literal[1]"
3031
+
takes_int_literal_1_2_3(MyEnum.TWO) # E: Argument 1 to "takes_int_literal_1_2_3" has incompatible type "Literal[MyEnum.TWO]"; expected "Literal[1, 2, 3]"
3032
+
3033
+
# Test assignments
3034
+
x: Literal[1] = MyIntEnum.ONE # OK
3035
+
y: Literal[1, 2, 3] = MyIntEnum.THREE # OK
3036
+
x2: Literal[1] = MyCustomIntEnum.ONE # OK
3037
+
y2: Literal[1, 2, 3] = MyCustomIntEnum.THREE # OK
3038
+
z: Literal[1] = MyEnum.ONE # E: Incompatible types in assignment (expression has type "Literal[MyEnum.ONE]", variable has type "Literal[1]")
3039
+
3040
+
# Test wrong values
3041
+
takes_int_literal_1(MyIntEnum.TWO) # E: Argument 1 to "takes_int_literal_1" has incompatible type "Literal[MyIntEnum.TWO]"; expected "Literal[1]"
3042
+
takes_int_literal_1(MyCustomIntEnum.TWO) # E: Argument 1 to "takes_int_literal_1" has incompatible type "Literal[MyCustomIntEnum.TWO]"; expected "Literal[1]"
3043
+
w: Literal[1] = MyIntEnum.THREE # E: Incompatible types in assignment (expression has type "Literal[MyIntEnum.THREE]", variable has type "Literal[1]")
3044
+
w2: Literal[1] = MyCustomIntEnum.THREE # E: Incompatible types in assignment (expression has type "Literal[MyCustomIntEnum.THREE]", variable has type "Literal[1]")
3045
+
3046
+
# Test reverse direction - literal ints should NOT be accepted where enum is expected
# StrEnum literals should be accepted where str literals are expected
3081
+
takes_str_literal_red(MyStrEnum.RED) # OK
3082
+
takes_str_literal_colors(MyStrEnum.GREEN) # OK
3083
+
3084
+
# Custom (str, Enum) literals should also be accepted
3085
+
takes_str_literal_red(MyCustomStrEnum.RED) # OK
3086
+
takes_str_literal_colors(MyCustomStrEnum.GREEN) # OK
3087
+
3088
+
# Regular Enum literals should not be accepted
3089
+
takes_str_literal_red(MyEnum.RED) # E: Argument 1 to "takes_str_literal_red" has incompatible type "Literal[MyEnum.RED]"; expected "Literal['red']"
3090
+
takes_str_literal_colors(MyEnum.GREEN) # E: Argument 1 to "takes_str_literal_colors" has incompatible type "Literal[MyEnum.GREEN]"; expected "Literal['red', 'green', 'blue']"
3091
+
3092
+
# Test assignments
3093
+
x: Literal["red"] = MyStrEnum.RED # OK
3094
+
y: Literal["red", "green", "blue"] = MyStrEnum.BLUE # OK
3095
+
x2: Literal["red"] = MyCustomStrEnum.RED # OK
3096
+
y2: Literal["red", "green", "blue"] = MyCustomStrEnum.BLUE # OK
3097
+
z: Literal["red"] = MyEnum.RED # E: Incompatible types in assignment (expression has type "Literal[MyEnum.RED]", variable has type "Literal['red']")
3098
+
3099
+
# Test wrong values
3100
+
takes_str_literal_red(MyStrEnum.GREEN) # E: Argument 1 to "takes_str_literal_red" has incompatible type "Literal[MyStrEnum.GREEN]"; expected "Literal['red']"
3101
+
takes_str_literal_red(MyCustomStrEnum.GREEN) # E: Argument 1 to "takes_str_literal_red" has incompatible type "Literal[MyCustomStrEnum.GREEN]"; expected "Literal['red']"
3102
+
w: Literal["red"] = MyStrEnum.BLUE # E: Incompatible types in assignment (expression has type "Literal[MyStrEnum.BLUE]", variable has type "Literal['red']")
3103
+
w2: Literal["red"] = MyCustomStrEnum.BLUE # E: Incompatible types in assignment (expression has type "Literal[MyCustomStrEnum.BLUE]", variable has type "Literal['red']")
3104
+
3105
+
# Test reverse direction - literal strings should NOT be accepted where enum is expected
0 commit comments