Skip to content

Commit 44d71eb

Browse files
committed
Also support list expressions.
1 parent 763c265 commit 44d71eb

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

docs/source/literal_types.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,15 @@ without a value:
372372
elif x == 'two':
373373
return False
374374
375-
For the sake of brevity, you can use the ``in`` operator in combination with tuple expressions
376-
(tuples created "on the fly"):
375+
For the sake of brevity, you can use the ``in`` operator in combination with list or tuple
376+
expressions (lists or tuples created "on the fly"):
377377

378378
.. code-block:: python
379379
380380
PossibleValues = Literal['one', 'two', 'three']
381381
382382
def validate(x: PossibleValues) -> bool:
383-
if x == 'one':
383+
if x in ['one']:
384384
return True
385385
elif x in ('two', 'three'):
386386
return False

mypy/checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4556,15 +4556,15 @@ def _make_tupleexpr_with_literals_narrowable_by_using_in(self, e: Expression) ->
45564556
isinstance(e, ComparisonExpr)
45574557
and isinstance(left := e.operands[0], NameExpr)
45584558
and ((op_in := e.operators[0]) in ("in", "not in"))
4559-
and isinstance(tuple_ := e.operands[1], TupleExpr)
4559+
and isinstance(litu := e.operands[1], (ListExpr, TupleExpr))
45604560
):
45614561
return e
45624562

45634563
op_eq, op_con = (["=="], "or") if (op_in == "in") else (["!="], "and")
45644564
line = e.line
45654565
left_new = left
45664566
comparisons = []
4567-
for right in reversed(tuple_.items):
4567+
for right in reversed(litu.items):
45684568
if isinstance(right, StarExpr):
45694569
return e
45704570
comparison = ComparisonExpr(op_eq, [left_new, right])

test-data/unit/check-narrowing.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ if isinstance(x, (Z, NoneType)): # E: Subclass of "X" and "Z" cannot exist: "Z"
20902090

20912091
[builtins fixtures/isinstance.pyi]
20922092

2093-
[case testNarrowLiteralsInTupleExpression]
2093+
[case testNarrowLiteralsInListOrTupleExpression]
20942094
# flags: --warn-unreachable
20952095

20962096
from typing import Optional
@@ -2101,9 +2101,9 @@ x: int
21012101
def f(v: Optional[Literal[1, 2, 3, 4]]) -> None:
21022102
if v in (0, 1, 2):
21032103
reveal_type(v) # N: Revealed type is "Union[Literal[1], Literal[2]]"
2104-
elif v in (1,):
2104+
elif v in [1]:
21052105
reveal_type(v) # E: Statement is unreachable
2106-
elif v is None or v in (3, x):
2106+
elif v is None or v in [3, x]:
21072107
reveal_type(v) # N: Revealed type is "Union[Literal[3], Literal[4], None]"
21082108
elif v in ():
21092109
reveal_type(v) # E: Statement is unreachable
@@ -2112,7 +2112,7 @@ def f(v: Optional[Literal[1, 2, 3, 4]]) -> None:
21122112
reveal_type(v) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3], Literal[4], None]"
21132113
[builtins fixtures/primitives.pyi]
21142114

2115-
[case testNarrowLiteralsNotInTupleExpression]
2115+
[case testNarrowLiteralsNotInListOrTupleExpression]
21162116
# flags: --warn-unreachable
21172117

21182118
from typing import Optional
@@ -2123,7 +2123,7 @@ x: int
21232123
def f(v: Optional[Literal[1, 2, 3, 4, 5]]) -> None:
21242124
if v not in (0, 1, 2, 3):
21252125
reveal_type(v) # N: Revealed type is "Union[Literal[4], Literal[5], None]"
2126-
elif v not in (1, 2, 3, 4): # E: Right operand of "and" is never evaluated
2126+
elif v not in [1, 2, 3, 4]: # E: Right operand of "and" is never evaluated
21272127
reveal_type(v) # E: Statement is unreachable
21282128
elif v is not None and v not in (3,):
21292129
reveal_type(v) # N: Revealed type is "Union[Literal[1], Literal[2]]"
@@ -2134,7 +2134,7 @@ def f(v: Optional[Literal[1, 2, 3, 4, 5]]) -> None:
21342134
reveal_type(v) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3], Literal[4], Literal[5], None]"
21352135
[builtins fixtures/primitives.pyi]
21362136

2137-
[case testNarrowEnumsInTupleExpression]
2137+
[case testNarrowEnumsInListOrTupleExpression]
21382138
from enum import Enum
21392139
from typing import Final
21402140

@@ -2151,7 +2151,7 @@ def f(v: E) -> None:
21512151
reveal_type(v) # N: Revealed type is "__main__.E"
21522152
if v in (A, E.B):
21532153
reveal_type(v) # N: Revealed type is "Union[Literal[__main__.E.A], Literal[__main__.E.B]]"
2154-
elif v in (E.A,):
2154+
elif v in [E.A]:
21552155
reveal_type(v)
21562156
elif v in (C,):
21572157
reveal_type(v) # N: Revealed type is "Literal[__main__.E.C]"
@@ -2162,7 +2162,7 @@ def f(v: E) -> None:
21622162
reveal_type(v) # N: Revealed type is "__main__.E"
21632163
[builtins fixtures/primitives.pyi]
21642164

2165-
[case testNarrowEnumsNotInTupleExpression]
2165+
[case testNarrowEnumsNotInListOrTupleExpression]
21662166
from enum import Enum
21672167
from typing import Final
21682168

@@ -2180,11 +2180,11 @@ def f(v: E) -> None:
21802180
reveal_type(v) # N: Revealed type is "__main__.E"
21812181
if v not in (A, E.B, E.C):
21822182
reveal_type(v) # N: Revealed type is "Union[Literal[__main__.E.D], Literal[__main__.E.E]]"
2183-
elif v not in (E.A, E.B, E.C, E.C):
2183+
elif v not in [E.A, E.B, E.C, E.C]:
21842184
reveal_type(v)
21852185
elif v not in (C,):
21862186
reveal_type(v) # N: Revealed type is "Union[Literal[__main__.E.A], Literal[__main__.E.B]]"
2187-
elif v not in ():
2187+
elif v not in []:
21882188
reveal_type(v) # N: Revealed type is "Literal[__main__.E.C]"
21892189
else:
21902190
reveal_type(v)

0 commit comments

Comments
 (0)