-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
bugmypy got something wrongmypy got something wrongtopic-reachabilityDetecting unreachable codeDetecting unreachable codetopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
When checking the following code:
from typing import AbstractSet, Iterable, Optional, Set, TypeVar
T = TypeVar('T')
def intersection(seq: Iterable[AbstractSet[T]]) -> Optional[Set[T]]:
ret = None
for elem in seq:
if ret is None:
ret = set(elem)
else:
ret &= elem
return retmypy reports:
$ mypy --warn-unreachable intersection.py
intersection.py:11: error: Statement is unreachable
This statement is the ret &= elem inside the else block.
It seems that mypy is using the initial type of ret to conclude that ret is None is always true, ignoring the fact that inside the loop ret is redefined to a different type.
If I place a reveal_type just before the return, mypy does report Union[builtins.set[T`-1], None] as the type, so it figures out the redefinition eventually.
I'm using mypy 0.770 on Pyton 3.8.2, with the --warn-unreachable flag.
karlb, samm0ss, d00astro, A5rocks, alichaudry and 1 more
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-reachabilityDetecting unreachable codeDetecting unreachable codetopic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder