Skip to content

False positive unreachable after type:ignore (also possibly invalid erorr code) #12785

Open
@mortoray

Description

@mortoray

In the below program, mypy still considers the return statement unreachable, even though the error on the previous line is ignored. I would expect that by stating the first error can be ignored, I wouldn't need to mark statements after it as ignore[unreachable]. But it appears, that even though mypy doesn't emit the first error, it still continues to trust its assessment of the flow control.

# Simulates a wrongly typed function in a types library (Redis get command)
def wrong_type_get() -> str|None:
	return "abc"
	
def get_int() -> int:
	value = wrong_type_get()
	assert isinstance(value, int) # type:ignore[unreachable]
	return value

MyPy emits the below, for the return value line.

sample.py:8: error: Statement is unreachable  [unreachable]

I can kind of understand why it happens, but I'm expecting MyPy to dismiss it's own knowledge when I state that it is wrong. Perhaps there is another comment I can put to that effect, like a # type:trustme:[not-unreachable]?


As an aside, the "unreachable" code being ignored seems wrong. The error message is correct, but with a a weird code. Removing the type:ignore above the output is:

error: Subclass of "str" and "int" cannot exist: would have incompatible method signatures  [unreachable]
$ env/bin/mypy --version
mypy 0.950 (compiled: yes)
$ env/bin/python --version
Python 3.10.4

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions