Skip to content

Generalize reachability checks to support enums #7000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 8, 2019
Prev Previous commit
Next Next commit
Merge branch 'master' into refine-enum-branch-analysis
  • Loading branch information
Michael0x2a authored Jun 24, 2019
commit 0c084dcb278974f2267c174d55ee1d03b49051ba
118 changes: 59 additions & 59 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ class SomeEnum(Enum):
[out]
main:2: note: Revealed type is 'builtins.int'
[out2]
main:2: error: Revealed type is 'builtins.str'
main:2: note: Revealed type is 'builtins.str'

[case testEnumReachabilityChecksBasic]
from enum import Enum
Expand All @@ -622,39 +622,39 @@ class Foo(Enum):

x: Literal[Foo.A, Foo.B, Foo.C]
if x is Foo.A:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
elif x is Foo.B:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.B]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.B]'
elif x is Foo.C:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.C]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.C]'
else:
reveal_type(x) # No output here: this branch is unreachable

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

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

if Foo.A is y:
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
elif Foo.B is y:
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.B]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.B]'
elif Foo.C is y:
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.C]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.C]'
else:
reveal_type(y) # No output here: this branch is unreachable
[builtins fixtures/bool.pyi]
Expand All @@ -675,45 +675,45 @@ y: Literal[Foo.A]
z: Final = Foo.A

if x is y:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
else:
reveal_type(x) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
if y is x:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
else:
reveal_type(x) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'

if x is z:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # E: Revealed type is '__main__.Foo'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # N: Revealed type is '__main__.Foo'
accepts_foo_a(z)
else:
reveal_type(x) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(z) # E: Revealed type is '__main__.Foo'
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(z) # N: Revealed type is '__main__.Foo'
accepts_foo_a(z)
if z is x:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # E: Revealed type is '__main__.Foo'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # N: Revealed type is '__main__.Foo'
accepts_foo_a(z)
else:
reveal_type(x) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(z) # E: Revealed type is '__main__.Foo'
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(z) # N: Revealed type is '__main__.Foo'
accepts_foo_a(z)

if y is z:
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # E: Revealed type is '__main__.Foo'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # N: Revealed type is '__main__.Foo'
accepts_foo_a(z)
else:
reveal_type(y) # No output: this branch is unreachable
reveal_type(z) # No output: this branch is unreachable
if z is y:
reveal_type(y) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # E: Revealed type is '__main__.Foo'
reveal_type(y) # N: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(z) # N: Revealed type is '__main__.Foo'
accepts_foo_a(z)
else:
reveal_type(y) # No output: this branch is unreachable
Expand All @@ -735,18 +735,18 @@ z: Literal[Foo.B, Foo.C]

# For the sake of simplicity, no narrowing is done when the narrower type is a Union.
if x is y:
reveal_type(x) # E: Revealed type is '__main__.Foo'
reveal_type(y) # E: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
reveal_type(x) # N: Revealed type is '__main__.Foo'
reveal_type(y) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
else:
reveal_type(x) # E: Revealed type is '__main__.Foo'
reveal_type(y) # E: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
reveal_type(x) # N: Revealed type is '__main__.Foo'
reveal_type(y) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'

if y is z:
reveal_type(y) # E: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
reveal_type(z) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(y) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
reveal_type(z) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
else:
reveal_type(y) # E: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
reveal_type(z) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
reveal_type(y) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Foo.B]]'
reveal_type(z) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C]]'
[builtins fixtures/bool.pyi]

[case testEnumReachabilityWithNone]
Expand All @@ -761,19 +761,19 @@ class Foo(Enum):

x: Optional[Foo]
if x:
reveal_type(x) # E: Revealed type is '__main__.Foo'
reveal_type(x) # N: Revealed type is '__main__.Foo'
else:
reveal_type(x) # E: Revealed type is 'Union[__main__.Foo, None]'
reveal_type(x) # N: Revealed type is 'Union[__main__.Foo, None]'

if x is not None:
reveal_type(x) # E: Revealed type is '__main__.Foo'
reveal_type(x) # N: Revealed type is '__main__.Foo'
else:
reveal_type(x) # E: Revealed type is 'None'
reveal_type(x) # N: Revealed type is 'None'

if x is Foo.A:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Foo.A]'
else:
reveal_type(x) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C], None]'
reveal_type(x) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Foo.C], None]'
[builtins fixtures/bool.pyi]

[case testEnumReachabilityWithMultipleEnums]
Expand All @@ -790,21 +790,21 @@ class Bar(Enum):

x1: Union[Foo, Bar]
if x1 is Foo.A:
reveal_type(x1) # E: Revealed type is 'Literal[__main__.Foo.A]'
reveal_type(x1) # N: Revealed type is 'Literal[__main__.Foo.A]'
else:
reveal_type(x1) # E: Revealed type is 'Union[Literal[__main__.Foo.B], __main__.Bar]'
reveal_type(x1) # N: Revealed type is 'Union[Literal[__main__.Foo.B], __main__.Bar]'

x2: Union[Foo, Bar]
if x2 is Bar.A:
reveal_type(x2) # E: Revealed type is 'Literal[__main__.Bar.A]'
reveal_type(x2) # N: Revealed type is 'Literal[__main__.Bar.A]'
else:
reveal_type(x2) # E: Revealed type is 'Union[__main__.Foo, Literal[__main__.Bar.B]]'
reveal_type(x2) # N: Revealed type is 'Union[__main__.Foo, Literal[__main__.Bar.B]]'

x3: Union[Foo, Bar]
if x3 is Foo.A or x3 is Bar.A:
reveal_type(x3) # E: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Bar.A]]'
reveal_type(x3) # N: Revealed type is 'Union[Literal[__main__.Foo.A], Literal[__main__.Bar.A]]'
else:
reveal_type(x3) # E: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Bar.B]]'
reveal_type(x3) # N: Revealed type is 'Union[Literal[__main__.Foo.B], Literal[__main__.Bar.B]]'

[builtins fixtures/bool.pyi]

Expand All @@ -823,13 +823,13 @@ def func(x: Union[int, None, Empty] = _empty) -> int:
# E: Unsupported left operand type for + ("Empty") \
# N: Left operand is of type "Union[int, None, Empty]"
if x is _empty:
reveal_type(x) # E: Revealed type is 'Literal[__main__.Empty.token]'
reveal_type(x) # N: Revealed type is 'Literal[__main__.Empty.token]'
return 0
elif x is None:
reveal_type(x) # E: Revealed type is 'None'
reveal_type(x) # N: Revealed type is 'None'
return 1
else: # At this point typechecker knows that x can only have type int
reveal_type(x) # E: Revealed type is 'builtins.int'
reveal_type(x) # N: Revealed type is 'builtins.int'
return x + 2
[builtins fixtures/primitives.pyi]

Expand All @@ -843,14 +843,14 @@ class Reason(Enum):

def process(response: Union[str, Reason] = '') -> str:
if response is Reason.timeout:
reveal_type(response) # E: Revealed type is 'Literal[__main__.Reason.timeout]'
reveal_type(response) # N: Revealed type is 'Literal[__main__.Reason.timeout]'
return 'TIMEOUT'
elif response is Reason.error:
reveal_type(response) # E: Revealed type is 'Literal[__main__.Reason.error]'
reveal_type(response) # N: Revealed type is 'Literal[__main__.Reason.error]'
return 'ERROR'
else:
# response can be only str, all other possible values exhausted
reveal_type(response) # E: Revealed type is 'builtins.str'
reveal_type(response) # N: Revealed type is 'builtins.str'
return 'PROCESSED: ' + response

[builtins fixtures/primitives.pyi]
You are viewing a condensed version of this merge commit. You can view the full changes here.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy