The --warn-unreachable check is reporting a false positive for a branch on an instance field that it thinks it knows the value of, but it cannot. This is when a function calls another member function prior to the branch.
from typing import *
class Unreachable:
def member_function(self) -> None:
self.path: Optional[str] = None
self.other()
if self.path:
print(self.path)
def other(self) -> None:
self.path = 'hi'
Unreachable().member_function()
The call to print(self.path) is marked as unreachable.
I presume this happens since mypy is seeing the None assignment to self.path, no intervening assignment, and thus assuming it is still None by the if self.path condition.
The call to self.other() however should invalidate any knowledge mypy has about self. instance variables. It cannot know how the other function modifies self.
Note: the error also happens inside an __init__ function, which is where I discovered it. I wanted to see if it was more general.
mypy: 0.770
The
--warn-unreachablecheck is reporting a false positive for a branch on an instance field that it thinks it knows the value of, but it cannot. This is when a function calls another member function prior to the branch.The call to
print(self.path)is marked as unreachable.I presume this happens since mypy is seeing the
Noneassignment toself.path, no intervening assignment, and thus assuming it is stillNoneby theif self.pathcondition.The call to
self.other()however should invalidate any knowledge mypy has aboutself.instance variables. It cannot know how the other function modifiesself.Note: the error also happens inside an
__init__function, which is where I discovered it. I wanted to see if it was more general.mypy: 0.770