Skip to content

Feature request - invoke any/all on non iterables #10404

@orSolocate

Description

@orSolocate

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:

  1. We know the value is non-iterable, then it is just an expansion of not-an-iterable for any and all. Since this also invokes not-an-iterable:
for _ in None:
    pass

Error description: same as in not-an-iterable: Non-iterable value %s is used in an iterating context .

  1. 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:

  1. Extension of not-an-iterable to any and all keyword.
  2. Enhancing not-an-iterable implementation to include cases where we know the value could be non-iterable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentNeeds specification 🔐Accepted as a potential improvement, and needs to specify edge cases, message names, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions