-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Closed
Copy link
Labels
bugmypy got something wrongmypy got something wrongtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
Running mypy --warn-unreachable (version 0.920) against the following snippet gives a false "unreachable statement" error.
from collections.abc import Hashable
from typing import List, Tuple, Union
def wrap_hash(obj: Union[Tuple[int], List[int]]):
if isinstance(obj, Hashable):
return hash(obj)
return hash(id(obj)) # error: Statement is unreachable [unreachable]
wrap_hash((0,)) # reaches 1st return
wrap_hash([0]) # reaches 2nd returnWe can even exchange the Union annotation for a single unhashable type like List or Dict, and still get the same unreachable error. Looks like isinstance(obj, Hashable) is just considered True for any type.
Is this a mypy bug? Can we add some hints into the example code so that mypy would figure out correct reachability?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder