Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New check: consider using any/all #5008

Closed
nickdrozd opened this issue Sep 14, 2021 · 4 comments · Fixed by #5196
Closed

New check: consider using any/all #5008

nickdrozd opened this issue Sep 14, 2021 · 4 comments · Fixed by #5196
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors
Milestone

Comments

@nickdrozd
Copy link
Contributor

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

@nickdrozd nickdrozd added Enhancement ✨ Improvement to a component Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 14, 2021
@Pierre-Sassoulas Pierre-Sassoulas added Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 14, 2021
@Pierre-Sassoulas
Copy link
Member

I like this one because it's something I often look for during review and this will definitely automate a part of my review process.

@jTaylo99
Copy link

jTaylo99 commented Oct 4, 2021

I'd like to give this a shot if that's alright.

@nickdrozd
Copy link
Contributor Author

@jTaylo99 Please, go right ahead!

@jTaylo99
Copy link

jTaylo99 commented Oct 5, 2021

Tried my luck for a few hours and made no headway, I think trying to build a new checker was a bit too big of a task for me to try and take on given I'm still working my head around the source code and astroid. Good luck to anyone else who wants to implement this as I think it'll be super helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors Hacktoberfest Help wanted 🙏 Outside help would be appreciated, good for new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants