Skip to content

Commit 3cda7db

Browse files
committed
Narrow disjoint narrows to Never
1 parent 4ddd34e commit 3cda7db

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6704,7 +6704,7 @@ def should_coerce_inner(typ: Type) -> bool:
67046704
if target and not is_same_type(target, expr_type):
67056705
# We have multiple disjoint target types. So the 'if' branch
67066706
# must be unreachable.
6707-
return None, {}
6707+
return {operands[j]: UninhabitedType() for j in chain_indices}, {}
67086708
target = expr_type
67096709
possible_target_indices.append(i)
67106710

test-data/unit/check-enum.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,15 +1325,15 @@ reveal_type(y) # N: Revealed type is "__main__.Foo"
13251325
# The standard output when we end up inferring two disjoint facts about the same expr
13261326
if x is Foo.A and x is Foo.B:
13271327
reveal_type(x) # E: Statement is unreachable \
1328-
# N: Revealed type is "__main__.Foo"
1328+
# N: Revealed type is "Never"
13291329
else:
13301330
reveal_type(x) # N: Revealed type is "__main__.Foo"
13311331
reveal_type(x) # N: Revealed type is "__main__.Foo"
13321332

13331333
# ..and we get the same result if we have two disjoint groups within the same comp expr
13341334
if x is Foo.A < x is Foo.B:
13351335
reveal_type(x) # E: Statement is unreachable \
1336-
# N: Revealed type is "__main__.Foo"
1336+
# N: Revealed type is "Never"
13371337
else:
13381338
reveal_type(x) # N: Revealed type is "__main__.Foo"
13391339
reveal_type(x) # N: Revealed type is "__main__.Foo"
@@ -1352,7 +1352,7 @@ class Foo(Enum):
13521352
x: Foo
13531353
if x is Foo.A is Foo.B:
13541354
reveal_type(x) # E: Statement is unreachable \
1355-
# N: Revealed type is "__main__.Foo"
1355+
# N: Revealed type is "Never"
13561356
else:
13571357
reveal_type(x) # N: Revealed type is "__main__.Foo"
13581358
reveal_type(x) # N: Revealed type is "__main__.Foo"
@@ -1361,7 +1361,7 @@ literal_a: Literal[Foo.A]
13611361
literal_b: Literal[Foo.B]
13621362
if x is literal_a is literal_b:
13631363
reveal_type(x) # E: Statement is unreachable \
1364-
# N: Revealed type is "__main__.Foo"
1364+
# N: Revealed type is "Never"
13651365
else:
13661366
reveal_type(x) # N: Revealed type is "__main__.Foo"
13671367
reveal_type(x) # N: Revealed type is "__main__.Foo"
@@ -1370,7 +1370,7 @@ final_a: Final = Foo.A
13701370
final_b: Final = Foo.B
13711371
if x is final_a is final_b:
13721372
reveal_type(x) # E: Statement is unreachable \
1373-
# N: Revealed type is "__main__.Foo"
1373+
# N: Revealed type is "Never"
13741374
else:
13751375
reveal_type(x) # N: Revealed type is "__main__.Foo"
13761376
reveal_type(x) # N: Revealed type is "__main__.Foo"

test-data/unit/check-narrowing.test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def test2(switch: FlipFlopEnum) -> None:
728728

729729
assert switch.state == State.B # E: Non-overlapping equality check (left operand type: "Literal[State.A]", right operand type: "Literal[State.B]")
730730
reveal_type(switch.state) # E: Statement is unreachable \
731-
# N: Revealed type is "Literal[__main__.State.A]"
731+
# N: Revealed type is "Never"
732732

733733
def test3(switch: FlipFlopEnum) -> None:
734734
# Same thing, but using 'is' comparisons. Previously mypy's behaviour differed
@@ -741,7 +741,9 @@ def test3(switch: FlipFlopEnum) -> None:
741741

742742
assert switch.state is State.B # E: Non-overlapping identity check (left operand type: "Literal[State.A]", right operand type: "Literal[State.B]")
743743
reveal_type(switch.state) # E: Statement is unreachable \
744-
# N: Revealed type is "Literal[__main__.State.A]"
744+
# N: Revealed type is "Never"
745+
746+
745747
[builtins fixtures/primitives.pyi]
746748

747749
[case testNarrowingEqualityRequiresExplicitStrLiteral]

test-data/unit/check-unreachable-code.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ def test_typed_fn(obj) -> None:
13651365

13661366
assert obj.prop is False
13671367
reveal_type(obj.prop) # E: Statement is unreachable \
1368-
# N: Revealed type is "Literal[True]"
1368+
# N: Revealed type is "Never"
13691369

13701370
[case testUnreachableCheckedUntypedFunction]
13711371
# flags: --warn-unreachable --check-untyped-defs
@@ -1378,7 +1378,7 @@ def test_untyped_fn(obj):
13781378

13791379
assert obj.prop is False
13801380
reveal_type(obj.prop) # E: Statement is unreachable \
1381-
# N: Revealed type is "Literal[True]"
1381+
# N: Revealed type is "Never"
13821382

13831383
[case testConditionalTypeVarException]
13841384
# every part of this test case was necessary to trigger the crash

0 commit comments

Comments
 (0)