Skip to content

Reachable code in loop marked unreachable #8721

Closed

Description

@PeterJCLaw
  • Are you reporting a bug, or opening a feature request?: bug
  • What is the actual behavior/output?

Given the following (cut down) loop code, mypy identifies incorrectly that the print statement cannot be reached.

last = None
reveal_type(last)  # Revealed type is 'None'

for val in range(10):
    reveal_type(last)  # Revealed type is 'None'

    if last is not None and last < 2:
        print("apparently unreachable")
        reveal_type(last)  # no output

    reveal_type(last)  # Revealed type is 'None'
    last = val
    reveal_type(last)  # Revealed type is 'builtins.int*'

My original code also used the value of last within the conditional; that was also warned about being not evaluated.

Notably, running mypy --no-warn-unreachable still doesn't give any output for the reveal_type in the if branch.

Happily this issue can be worked around by adding an explicit Optional[int] annotation to last.

  • What is the behavior/output you expect?

I think mypy should consider last to be Optional[int] in the early part of the loop, rather than just being None. That would fix the unreachable warning as well as potentially being more correct.

Within the if last is not None branch, last should be of type int.

  • What are the versions of mypy and Python you are using? mypy 0.770, Python 3.5.2
    Do you see the same issue after installing mypy from Git master? yes (tested at 125728a)
  • What are the mypy flags you are using? --warn-unreachable

Activity

  1. added a commit that references this issue on Apr 25, 2020
  2. JukkaL commented on May 1, 2020

    @JukkaL
    Collaborator

    This seems pretty bad, as it can result in false negatives. Mypy should give an error when reading the value of last before it has a non-None type, at least within a loop.

  3. sbalmer commented on Mar 9, 2021

    @sbalmer

    I think #8865 is about the same problem.

  4. davidhalter commented on Aug 4, 2024

    @davidhalter

    False positives are also possible without partials, see for example:

    https://mypy-play.net/?mypy=latest&python=3.12&flags=warn-unreachable&gist=10808f2ddaa8e35da240e50b068104f3

    from typing import *                                                            
    foo: Union[int, str]                                                            
    foo = ""                                                                        
                                                                                    
    while bool():                                                                   
        reveal_type(foo)                                                            
        if isinstance(foo, str):                                                    
            foo = 1                                                                 
            continue                                                                
        else:                                                                       
            foo = ""                                                                                 
            continue
    
  5. added a commit that references this issue on Jan 9, 2025
    53d894a
  6. added a commit that references this issue on Jan 12, 2025
    9685171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions