You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
I've encountered similar issues myself, though I can't remember if there has been any online discussion about these. Stricter boolean checks would at least be nice as an opt-in check.
This is a bit of a long shot, because this isn't really a type error per se. But consider the following:
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:
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 anif
orwhile
?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.
The text was updated successfully, but these errors were encountered: