Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Reachable code in loop marked unreachable #8721

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

Closed
PeterJCLaw opened this issue Apr 25, 2020 · 3 comments · Fixed by #18433
Closed

Reachable code in loop marked unreachable #8721

PeterJCLaw opened this issue Apr 25, 2020 · 3 comments · Fixed by #18433
Labels
bug mypy got something wrong priority-0-high topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder

Comments

@PeterJCLaw
Copy link
Contributor

PeterJCLaw commented Apr 25, 2020

  • 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
PeterJCLaw added a commit to PeterJCLaw/srcomp that referenced this issue Apr 25, 2020
@JukkaL
Copy link
Collaborator

JukkaL commented May 1, 2020

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.

@JukkaL JukkaL added bug mypy got something wrong priority-0-high labels May 1, 2020
@sbalmer
Copy link

sbalmer commented Mar 9, 2021

I think #8865 is about the same problem.

@davidhalter
Copy link

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

@hauntsaninja hauntsaninja added the topic-type-narrowing Conditional type narrowing / binder label Nov 18, 2024
tyralla added a commit to tyralla/mypy that referenced this issue Jan 9, 2025
Fixes python#18348
Fixes python#13973
Fixes python#11612
Fixes python#8721
Fixes python#8865
Fixes python#7204

I manually checked all the listed issues.  Some of them were already partly fixed by python#18180.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants