8000 Add --warn-unreachable to more tests (#20977) · python/mypy@32f0502 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32f0502

Browse files
authored
Add --warn-unreachable to more tests (#20977)
1 parent 3b39b1b commit 32f0502

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

test-data/unit/check-enum.test

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ main:2: note: Revealed type is "Literal[1]?"
866866
main:2: note: Revealed type is "Literal['foo']?"
867867

868868
[case testEnumReachabilityChecksBasic]
869+
# flags: --warn-unreachable
869870
from enum import Enum
870871
from typing import Literal
871872

@@ -882,7 +883,7 @@ elif x is Foo.B:
882883
elif x is Foo.C:
883884
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.C]"
884885
else:
885-
reveal_type(x) # No output here: this branch is unreachable
886+
reveal_type(x) # E: Statement is unreachable
886887
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B] | Literal[__main__.Foo.C]"
887888

888889
if Foo.A is x:
@@ -892,7 +893,7 @@ elif Foo.B is x:
892893
elif Foo.C is x:
893894
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.C]"
894895
else:
895-
reveal_type(x) # No output here: this branch is unreachable
896+
reveal_type(x) # E: Statement is unreachable
896897
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B] | Literal[__main__.Foo.C]"
897898

898899
y: Foo
@@ -903,7 +904,7 @@ elif y is Foo.B:
903904
elif y is Foo.C:
904905
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.C]"
905906
else:
906-
reveal_type(y) # No output here: this branch is unreachable
907+
reveal_type(y) # E: Statement is unreachable
907908
reveal_type(y) # N: Revealed type is "__main__.Foo"
908909

909910
if Foo.A is y:
@@ -913,11 +914,12 @@ elif Foo.B is y:
913914
elif Foo.C is y:
914915
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.C]"
915916
else:
916-
reveal_type(y) # No output here: this branch is unreachable
917+
reveal_type(y) # E: Statement is unreachable
917918
reveal_type(y) # N: Revealed type is "__main__.Foo"
918919
[builtins fixtures/bool.pyi]
919920

920921
[case testEnumReachabilityChecksWithOrdering]
922+
# flags: --warn-unreachable
921923
from enum import Enum
922924
from typing import Literal
923925

@@ -934,7 +936,7 @@ if x is Foo.A:
934936
elif x is Foo.B:
935937
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.B]"
936938
else:
937-
reveal_type(x) # No output here: this branch is unreachable
939+
reveal_type(x) # E: Statement is unreachable
938940

939941
class Bar(Enum):
940942
__order__ = "A B"
@@ -949,26 +951,27 @@ if y is Bar.A:
949951
elif y is Bar.B:
950952
reveal_type(y) # N: Revealed type is "Literal[__main__.Bar.B]"
951953
else:
952-
reveal_type(y) # No output here: this branch is unreachable< 10BC0 /div>
954+
reveal_type(y) # E: Statement is unreachable
953955

954956
x2: Foo
955957
if x2 is Foo.A:
956958
reveal_type(x2) # N: Revealed type is "Literal[__main__.Foo.A]"
957959
elif x2 is Foo.B:
958960
reveal_type(x2) # N: Revealed type is "Literal[__main__.Foo.B]"
959961
else:
960-
reveal_type(x2) # No output here: this branch is unreachable
962+
reveal_type(x2) # E: Statement is unreachable
961963

962964
y2: Bar
963965
if y2 is Bar.A:
964966
reveal_type(y2) # N: Revealed type is "Literal[__main__.Bar.A]"
965967
elif y2 is Bar.B:
966968
reveal_type(y2) # N: Revealed type is "Literal[__main__.Bar.B]"
967969
else:
968-
reveal_type(y2) # No output here: this branch is unreachable
970+
reveal_type(y2) # E: Statement is unreachable
969971
[builtins fixtures/tuple.pyi]
970972

971973
[case testEnumReachabilityChecksIndirect]
974+
# flags: --warn-unreachable
972975
from enum import Enum
973976
from typing import Final, Literal
974977

@@ -1022,18 +1025,19 @@ if y is z:
10221025
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?"
10231026
accepts_foo_a(z)
10241027
else:
1025-
reveal_type(y) # No output: this branch is unreachable
1026-
reveal_type(z) # No output: this branch is unreachable
1028+
reveal_type(y) # E: Statement is unreachable
1029+
reveal_type(z)
10271030
if z is y:
10281031
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]"
10291032
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?"
10301033
accepts_foo_a(z)
10311034
else:
1032-
reveal_type(y) # No output: this branch is unreachable
1033-
reveal_type(z) # No output: this branch is unreachable
1035+
reveal_type(y) # E: Statement is unreachable
1036+
reveal_type(z)
10341037
[builtins fixtures/bool.pyi]
10351038

10361039
[case testEnumReachabilityNarrowingForUnionMessiness]
1040+
# flags: --warn-unreachable
10371041
from enum import Enum
10381042
from typing import Literal
10391043

@@ -1062,6 +1066,7 @@ else:
10621066
[builtins fixtures/bool.pyi]
10631067

10641068
[case testEnumReachabilityWithNone]
1069+
# flags: --warn-unreachable
10651070
from enum import Enum
10661071
from typing import Optional
10671072

@@ -1089,6 +1094,7 @@ reveal_type(x) # N: Revealed type is "__main__.Foo | None"
10891094
[builtins fixtures/enum.pyi]
10901095

10911096
[case testEnumReachabilityWithMultipleEnums]
1097+
# flags: --warn-unreachable
10921098
from enum import Enum
10931099
from typing import Literal, Union
10941100

@@ -1123,6 +1129,7 @@ reveal_type(x3) # N: Revealed type is "__main__.Foo | __main__.Bar"
11231129
[builtins fixtures/bool.pyi]
11241130

11251131
[case testEnumReachabilityPEP484ExampleWithFinal]
1132+
# flags: --warn-unreachable
11261133
from typing import Final, Union
11271134
from enum import Enum
11281135

@@ -1146,6 +1153,7 @@ def func(x: Union[int, None, Empty] = _empty) -> int:
11461153
[builtins fixtures/primitives.pyi]
11471154

11481155
[case testEnumReachabilityPEP484ExampleWithMultipleValues]
1156+
# flags: --warn-unreachable
11491157
from typing import Union
11501158
from enum import Enum
11511159

@@ -1168,6 +1176,7 @@ def process(response: Union[str, Reason] = '') -> str:
11681176

11691177

11701178
[case testEnumReachabilityPEP484ExampleSingleton]
1179+
# flags: --warn-unreachable
11711180
from typing import Final, Union
11721181
from enum import Enum
11731182

@@ -1191,7 +1200,7 @@ def func(x: Union[int, None, Empty] = _empty) -> int:
11911200
[builtins fixtures/primitives.pyi]
11921201

11931202
[case testEnumReachabilityPEP484ExampleSingletonWithMethod]
1194-
# flags: --python-version 3.11
1203+
# flags: --python-version 3.11 --warn-unreachable
11951204
from typing import Final, Union
11961205
from enum import Enum, member
11971206

@@ -1232,6 +1241,7 @@ reveal_type(A().b) # N: Revealed type is "Any"
12321241
[builtins fixtures/enum.pyi]
12331242

12341243
[case testEnumReachabilityWithChaining]
1244+
# flags: --warn-unreachable
12351245
from enum import Enum
12361246

12371247
class Foo(Enum):

test-data/unit/check-functions.test

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,15 +1523,16 @@ else:
15231523
# N: def f(int, /) -> None
15241524

15251525
[case testConditionalFunctionDefinitionUnreachable]
1526+
# flags: --warn-unreachable
15261527
def bar() -> None:
15271528
if False:
1528-
foo = 1
1529+
foo = 1 # E: Statement is unreachable
15291530
else:
15301531
def foo(obj): ...
15311532

15321533
def baz() -> None:
15331534
if False:
1534-
foo: int = 1
1535+
foo: int = 1 # E: Statement is unreachable
15351536
else:
15361537
def foo(obj): ... # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type "int")
15371538
[builtins fixtures/tuple.pyi]

test-data/unit/check-protocols.test

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,7 @@ if isinstance(x, Iterable):
17291729
[typing fixtures/typing-full.pyi]
17301730

17311731
[case testConcreteClassesInProtocolsIsInstance]
1732+
# flags: --warn-unreachable
17321733
from typing import Protocol, runtime_checkable, TypeVar, Generic
17331734

17341735
T = TypeVar('T')
@@ -1757,25 +1758,25 @@ c = C()
17571758
if isinstance(c, P1):
17581759
reveal_type(c) # N: Revealed type is "__main__.C"
17591760
else:
1760-
reveal_type(c) # Unreachable
1761+
reveal_type(c) # E: Statement is unreachable
17611762
if isinstance(c, P):
17621763
reveal_type(c) # N: Revealed type is "__main__.C"
17631764
else:
1764-
reveal_type(c) # Unreachable
1765+
reveal_type(c) # E: Statement is unreachable
17651766

17661767
c1i: C1[int]
17671768
if isinstance(c1i, P1):
17681769
reveal_type(c1i) # N: Revealed type is "__main__.C1[builtins.int]"
17691770
else:
1770-
reveal_type(c1i) # Unreachable
1771+
reveal_type(c1i) # E: Statement is unreachable
17711772
if isinstance(c1i, P):
17721773
reveal_type(c1i) # N: Revealed type is "__main__.<subclass of "__main__.C1[builtins.int]" and "__main__.P">"
17731774
else:
17741775
reveal_type(c1i) # N: Revealed type is "__main__.C1[builtins.int]"
17751776

17761777
c1s: C1[str]
1777-
if isinstance(c1s, P1):
1778-
reveal_type(c1s) # Unreachable
1778+
if isinstance(c1s, P1): # E: Subclass of "C1[str]" and "P1" cannot exist: would have incompatible method signatures
1779+
reveal_type(c1s) # E: Statement is unreachable
17791780
else:
17801781
reveal_type(c1s) # N: Revealed type is "__main__.C1[builtins.str]"
17811782

0 commit comments

Comments
 (0)
0