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

Type Inference with for loop incorrect #8272

Closed
alanhdu opened this issue Jan 10, 2020 · 1 comment
Closed

Type Inference with for loop incorrect #8272

alanhdu opened this issue Jan 10, 2020 · 1 comment

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Jan 10, 2020

Using mypy 0.761 on Python 3.6, the following code, running mypy --warn-unreachable on the following code fails:

from typing import Sequence, List


def remove_duplicates(xs: Sequence[int]) -> Sequence[int]:
    output: List[int] = []

    prev = None
    for x in xs:
        reveal_type(prev)

        if prev == x:
            continue
        elif prev is not None and prev != 999:
            output.append(prev)
        prev = x

        reveal_type(prev)
    reveal_type(prev)
    return output

with

$ mypy --warn-unreachable scratch.py 
scratch.py:9: note: Revealed type is 'None'
scratch.py:13: error: Right operand of 'and' is never evaluated
scratch.py:14: error: Statement is unreachable
scratch.py:17: note: Revealed type is 'builtins.int*'
scratch.py:18: note: Revealed type is 'Union[builtins.int*, None]'

This is clearly false -- it is possible for the prev != 999 to actually run. The reveal_types indicate that this has something to do with type inference: although mypy successfully infers that prev is Optional[int] after the for loop, it does not combine the int and None types during the for loop.

@msullivan
Copy link
Collaborator

Yeah this is a bad one. Duplicate of #5423. Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants