@@ -3201,7 +3201,7 @@ class X:
32013201[builtins fixtures/dict.pyi]
32023202
32033203
3204- [case testTypeNarrowingStringInLiteralContainer ]
3204+ [case testNarrowStringInLiteralContainer ]
32053205# flags: --strict-equality --warn-unreachable
32063206from typing import Literal
32073207
@@ -3235,6 +3235,69 @@ def narrow_set(x: str, t: set[Literal['a', 'b']]):
32353235 reveal_type(x) # N: Revealed type is "builtins.str"
32363236[builtins fixtures/primitives.pyi]
32373237
3238+ [case testNarrowLiteralInLiteralContainer]
3239+ # flags: --strict-equality --warn-unreachable
3240+ from typing import Literal
3241+
3242+ def narrow_tuple_exact(x: Literal['a', 'b', 'c'], t: tuple[Literal['a'], Literal['b']]):
3243+ if x in t:
3244+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3245+ else:
3246+ reveal_type(x) # N: Revealed type is "Literal['c']"
3247+
3248+ if x not in t:
3249+ reveal_type(x) # N: Revealed type is "Literal['c']"
3250+ else:
3251+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3252+
3253+ def narrow_tuple_expression(x: Literal['a', 'b', 'c']):
3254+ # TODO: this should match narrow_tuple_exact
3255+ if x in ('a', 'b'):
3256+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3257+ else:
3258+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3259+
3260+ if x not in ('a', 'b'):
3261+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3262+ else:
3263+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3264+
3265+ def narrow_tuple_union(x: Literal['a', 'b', 'c'], t: tuple[Literal['a', 'b']]):
3266+ if x in t:
3267+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3268+ else:
3269+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3270+
3271+ if x not in t:
3272+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3273+ else:
3274+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3275+
3276+ def narrow_tuple_with_other_type(x: Literal['a', 'b', 'c'], t: tuple[Literal['a'], int]):
3277+ if x in t:
3278+ reveal_type(x) # N: Revealed type is "Literal['a']"
3279+ else:
3280+ reveal_type(x) # N: Revealed type is "Literal['b'] | Literal['c']"
3281+
3282+ def narrow_homo_tuple(x: Literal['a', 'b', 'c'], t: tuple[Literal['a', 'b'], ...]):
3283+ if x in t:
3284+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3285+ else:
3286+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3287+
3288+ def narrow_list(x: Literal['a', 'b', 'c'], t: list[Literal['a', 'b']]):
3289+ if x in t:
3290+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3291+ else:
3292+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3293+
3294+ def narrow_set(x: Literal['a', 'b', 'c'], t: set[Literal['a', 'b']]):
3295+ if x in t:
3296+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
3297+ else:
3298+ reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
3299+ [builtins fixtures/primitives.pyi]
3300+
32383301
32393302[case testNarrowingLiteralInLiteralContainer]
32403303# flags: --strict-equality --warn-unreachable
0 commit comments