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

False positive: Statement is unreachable [unreachable] #12784

Closed
mortoray opened this issue May 14, 2022 · 1 comment
Closed

False positive: Statement is unreachable [unreachable] #12784

mortoray opened this issue May 14, 2022 · 1 comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

Comments

@mortoray
Copy link

Bug Report

This code causes mypy to emit an [unreachable] error.

def reader(value):
	last_msg_id = None
	for message_raw in [1,2]:
		msg_id = value
		if last_msg_id is None:
			last_msg_id = msg_id
		else:
			print("here")
				
reader(1)
$ env/bin/mypy /tmp/sample.py 
/tmp/sample.py:8: error: Statement is unreachable  [unreachable]

Running the program emits "here", so clearly it is reachable. Logical analysis also indicates it is reachable.

$ env/bin/mypy --version
mypy 0.950 (compiled: yes)
$ env/bin/python --version
Python 3.10.4

I'm not sure what is special about this structure that triggers the false positive. It's a reduced down version of a websocket server I have, where the code looks more like this:

import json

def decode_msg(text: str) -> dict:
	return json.loads(text)

async def reader(websocket):
	last_msg_id = None
	async for message_raw in websocket:
		msg = decode_msg(message_raw)
		msg_id = msg['msg_id']
		if last_msg_id is None:
			last_msg_id = msg_id
		else:
			if msg_id != last_msg_id+1:
				print("bad")

Which also generates the error.

@mortoray mortoray added the bug mypy got something wrong label May 14, 2022
@AlexWaygood AlexWaygood added the topic-reachability Detecting unreachable code label May 14, 2022
@AlexWaygood
Copy link
Member

Duplicate of #11612

@AlexWaygood AlexWaygood marked this as a duplicate of #11612 May 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code
Projects
None yet
Development

No branches or pull requests

2 participants