Skip to content

invalid knowledge of self variables retained after self call, unreachable false positive, invalid operations allowed #10151

@mortoray

Description

@mortoray

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions