-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make reachability code understand chained comparisons
Currently, our reachability code does not understand how to parse comparisons like `a == b == c`: the `find_isinstance_check` method only attempts to analyze comparisons that contain a single `==`, `is`, or `in` operator. This pull request generalizes that logic so we can support any arbitrary number of comparisons. It also along the way unifies the logic we have for handling `is` and `==` checks: the latter check is now just treated a weaker variation of the former. (Expressions containing `==` may do arbitrary things if the underlying operands contain custom `__eq__` methods.) As a side-effect, this PR adds support for the following: x: Optional[str] if x is 'some-string': # Previously, the revealed type would be Union[str, None] # Now, the revealed type is just 'str' reveal_type(x) else: reveal_type(x) # N: Revealed type is 'Union[builtins.str, None]' We previously supported this narrowing logic when doing equality checks (e.g. when doing `if x == 'some-string'`). As a second side-effect, this PR adds support for the following: class Foo(Enum): A = 1 B = 2 y: Foo if y == Foo.A: reveal_type(y) # N: Revealed type is 'Literal[Foo.A]' else: reveal_type(y) # N: Revealed type is 'Literal[Foo.B]' We previously supported this kind of narrowing only when doing identity checks (e.g. `if y is Foo.A`). To avoid any bad interactions with custom `__eq__` methods, we enable this narrowing check only if both operands do not define custom `__eq__` methods.
- Loading branch information
1 parent
5b70ff5
commit 87f1571
Showing
5 changed files
with
298 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.