Closed
Description
Similar to this #6027, but more basic use case.
from typing import Collection, Union, List
def foo(bar: Union[bool, Collection[int]]) -> Union[int, List[int]]:
if bar is False: # or if not bar:
return 10
if bar is True:
return 20
# otherwise, bar must be a collection (list, tuple, ...)
return [i*2 for i in bar]
print(foo(True))
print(foo(False))
print(foo([1, 2, 3]))
print(foo((1, 2, 3,)))
Mypy is giving the error
error: Item "bool" of "Union[bool, Collection[int]]" has no attribute "__iter__" (not iterable)
But it should be able to infer that it can't be a bool
?
using if / elif /else also gives the error.