Description
This is a bit of a long shot, because this isn't really a type error per se. But consider the following:
numbers: List[int] = []
my_generator: Iterable = (i for i in numbers)
if not my_generator:
print("empty")
Obviously this will not print "empty", because a is a generator
, which is always truthy. However, 99.9% of the times this is not what the user intended, and it would be great if a tool could identify it and warn about it, and mypy seems well-placed to be able to do it.
Beyond the fact that this isn't really a type "error", there are some more complications. For example, how to more generally define what objects don't have a "significant" boolean representation. One could argue that this is just as much a "mistake" as the example with the generator:
class A:
pass
a: A = A()
if not a:
print("empty")
As well as the question of what's considered "using something in a boolean context". Should calling bool()
on a generator be the same as using it in the conditional expression of an if
or while
?
So I'm not at all sure this is a feasible or reasonable suggestion, but I didn't find any mention if it in past issues, so thought it was worth bringing up for discussion.