Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ main:2: note: Revealed type is "Literal[1]?"
main:2: note: Revealed type is "Literal['foo']?"

[case testEnumReachabilityChecksBasic]
# flags: --warn-unreachable
from enum import Enum
from typing import Literal

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

if Foo.A is x:
Expand All @@ -892,7 +893,7 @@ elif Foo.B is x:
elif Foo.C is x:
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.C]"
else:
reveal_type(x) # No output here: this branch is unreachable
reveal_type(x) # E: Statement is unreachable
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B] | Literal[__main__.Foo.C]"

y: Foo
Expand All @@ -903,7 +904,7 @@ elif y is Foo.B:
elif y is Foo.C:
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.C]"
else:
reveal_type(y) # No output here: this branch is unreachable
reveal_type(y) # E: Statement is unreachable
reveal_type(y) # N: Revealed type is "__main__.Foo"

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

[case testEnumReachabilityChecksWithOrdering]
# flags: --warn-unreachable
from enum import Enum
from typing import Literal

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

class Bar(Enum):
__order__ = "A B"
Expand All @@ -949,26 +951,27 @@ if y is Bar.A:
elif y is Bar.B:
reveal_type(y) # N: Revealed type is "Literal[__main__.Bar.B]"
else:
reveal_type(y) # No output here: this branch is unreachable
reveal_type(y) # E: Statement is unreachable

x2: Foo
if x2 is Foo.A:
reveal_type(x2) # N: Revealed type is "Literal[__main__.Foo.A]"
elif x2 is Foo.B:
reveal_type(x2) # N: Revealed type is "Literal[__main__.Foo.B]"
else:
reveal_type(x2) # No output here: this branch is unreachable
reveal_type(x2) # E: Statement is unreachable

y2: Bar
if y2 is Bar.A:
reveal_type(y2) # N: Revealed type is "Literal[__main__.Bar.A]"
elif y2 is Bar.B:
reveal_type(y2) # N: Revealed type is "Literal[__main__.Bar.B]"
else:
reveal_type(y2) # No output here: this branch is unreachable
reveal_type(y2) # E: Statement is unreachable
[builtins fixtures/tuple.pyi]

[case testEnumReachabilityChecksIndirect]
# flags: --warn-unreachable
from enum import Enum
from typing import Final, Literal

Expand Down Expand Up @@ -1022,18 +1025,19 @@ if y is z:
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?"
accepts_foo_a(z)
else:
reveal_type(y) # No output: this branch is unreachable
reveal_type(z) # No output: this branch is unreachable
reveal_type(y) # E: Statement is unreachable
reveal_type(z)
if z is y:
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]"
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.A]?"
accepts_foo_a(z)
else:
reveal_type(y) # No output: this branch is unreachable
reveal_type(z) # No output: this branch is unreachable
reveal_type(y) # E: Statement is unreachable
reveal_type(z)
[builtins fixtures/bool.pyi]

[case testEnumReachabilityNarrowingForUnionMessiness]
# flags: --warn-unreachable
from enum import Enum
from typing import Literal

Expand Down Expand Up @@ -1062,6 +1066,7 @@ else:
[builtins fixtures/bool.pyi]

[case testEnumReachabilityWithNone]
# flags: --warn-unreachable
from enum import Enum
from typing import Optional

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

[case testEnumReachabilityWithMultipleEnums]
# flags: --warn-unreachable
from enum import Enum
from typing import Literal, Union

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

[case testEnumReachabilityPEP484ExampleWithFinal]
# flags: --warn-unreachable
from typing import Final, Union
from enum import Enum

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

[case testEnumReachabilityPEP484ExampleWithMultipleValues]
# flags: --warn-unreachable
from typing import Union
from enum import Enum

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


[case testEnumReachabilityPEP484ExampleSingleton]
# flags: --warn-unreachable
from typing import Final, Union
from enum import Enum

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

[case testEnumReachabilityPEP484ExampleSingletonWithMethod]
# flags: --python-version 3.11
# flags: --python-version 3.11 --warn-unreachable
from typing import Final, Union
from enum import Enum, member

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

[case testEnumReachabilityWithChaining]
# flags: --warn-unreachable
from enum import Enum

class Foo(Enum):
Expand Down
5 changes: 3 additions & 2 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1523,15 +1523,16 @@ else:
# N: def f(int, /) -> None

[case testConditionalFunctionDefinitionUnreachable]
# flags: --warn-unreachable
def bar() -> None:
if False:
foo = 1
foo = 1 # E: Statement is unreachable
else:
def foo(obj): ...

def baz() -> None:
if False:
foo: int = 1
foo: int = 1 # E: Statement is unreachable
else:
def foo(obj): ... # E: Incompatible redefinition (redefinition with type "Callable[[Any], Any]", original type "int")
[builtins fixtures/tuple.pyi]
Expand Down
11 changes: 6 additions & 5 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,7 @@ if isinstance(x, Iterable):
[typing fixtures/typing-full.pyi]

[case testConcreteClassesInProtocolsIsInstance]
# flags: --warn-unreachable
from typing import Protocol, runtime_checkable, TypeVar, Generic

T = TypeVar('T')
Expand Down Expand Up @@ -1757,25 +1758,25 @@ c = C()
if isinstance(c, P1):
reveal_type(c) # N: Revealed type is "__main__.C"
else:
reveal_type(c) # Unreachable
reveal_type(c) # E: Statement is unreachable
if isinstance(c, P):
reveal_type(c) # N: Revealed type is "__main__.C"
else:
reveal_type(c) # Unreachable
reveal_type(c) # E: Statement is unreachable

c1i: C1[int]
if isinstance(c1i, P1):
reveal_type(c1i) # N: Revealed type is "__main__.C1[builtins.int]"
else:
reveal_type(c1i) # Unreachable
reveal_type(c1i) # E: Statement is unreachable
if isinstance(c1i, P):
reveal_type(c1i) # N: Revealed type is "__main__.<subclass of "__main__.C1[builtins.int]" and "__main__.P">"
else:
reveal_type(c1i) # N: Revealed type is "__main__.C1[builtins.int]"

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

Expand Down