Skip to content

New check: consider using any/all #5008

Closed
@nickdrozd

Description

@nickdrozd

Current problem

Here is a function from Astroid:

def is_from_decorator(node):
    """Return True if the given node is the child of a decorator"""
    for parent in node.node_ancestors():
        if isinstance(parent, Decorators):
            return True
    return False

What does it do? Evidently it loops over the node's ancestors and returns whether any of them are decorators. Well, why not just say that?

def is_from_decorator(node):
    """Return True if the given node is the child of a decorator"""
    return any(isinstance(parent, Decorators)
               for parent in node.node_ancestors())

Desired solution

The any and all functions are generally shorter and more declarative than explicit looping, so add a check to suggest using them when appropriate.

Implmenting this check so as to avoid too many false positives might be an interesting challenge!

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement ✨Improvement to a componentGood first issueFriendly and approachable by new contributorsHacktoberfestHelp wanted 🙏Outside help would be appreciated, good for new contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions