-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Current problem
Probably an extension to the existing not-an-iterable
error message.
Looks pretty trivial, but could be very useful in cases we want to run any
or all
on a generator or list that is a return value of a function call that might also return None
(e.g. in a side/error case). Running any(None)
or all(None)
causes Python to crush, the user might think the code is okay, without ever running the None
case, and the error is discovered only in a later stage.
Pylint shows no errors (as far as I am concerned) for:
all(None)
any(None)
def create_list():
if error_case():
return None
return [1, 2, 3]
all(create_list())
any(create_list())
for _ in create_list():
pass
Desired solution
I think we have here 2 cases:
- We know the value is non-iterable, then it is just an expansion of
not-an-iterable
forany
andall
. Since this also invokesnot-an-iterable
:
for _ in None:
pass
Error description: same as in not-an-iterable
: Non-iterable value %s is used in an iterating context
.
- In some cases we know the value is non-iterable, then we need an extended logic that could also be applicable to using the
for
keyboard, e.g.:
def create_list():
if error_case():
return None
return [1, 2, 3]
for _ in create_list():
pass
Error description: might be the same error name as not-an-iterable
, but with different description: Non-iterable value %s might be used in an iterating context
(emphasis on the might).
Additional context
Maybe there are more cases of a built in function that causes iteration apart from all
/any
/for
. Note that we might want to split this issue into 2 issues:
- Extension of
not-an-iterable
toany
andall
keyword. - Enhancing
not-an-iterable
implementation to include cases where we know the value could be non-iterable.