Skip to content
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

Make reachability code understand chained comparisons #7169

Closed

Commits on Jul 8, 2019

  1. 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.
    Michael0x2a committed Jul 8, 2019
    Configuration menu
    Copy the full SHA
    90418df View commit details
    Browse the repository at this point in the history

Commits on Aug 22, 2019

  1. Configuration menu
    Copy the full SHA
    fe454ad View commit details
    Browse the repository at this point in the history