Narrowing an optional class type to an unrelated Protocol causes a false unreachable error #12802
Labels
bug
mypy got something wrong
topic-protocols
topic-reachability
Detecting unreachable code
topic-type-narrowing
Conditional type narrowing / binder
Bug Report
If
Y
is a runtime-checkable protocol which is not implemented by the classX
, then mypy will erroneously consider the print statement in the following to be unreachable:This is incorrect, because
x
may be an object created from a subclass ofX
which does implementY
.If
x
is annotated asX
(notOptional[X]
), then mypy behaves correctly.To Reproduce
Save the following source code to a file (called
foo.py
below).Run
mypy
on it with--warn-unreachable
Expected Behavior
Actual Behavior
(Running the script demonstrates that there is no actual unreachable code at runtime.)
Your Environment
mypy 0.950 (compiled: yes)
--warn-unreachable
mypy.ini
(and other config files): NonePython 3.10.4
The text was updated successfully, but these errors were encountered: