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

False positive unnecessary-comprehension when modifying a dict to a list of tuples #4499

Closed
martimlobao opened this issue May 24, 2021 · 4 comments · Fixed by #4500
Closed
Assignees
Labels
Documentation 📗 Good first issue Friendly and approachable by new contributors Help wanted 🙏 Outside help would be appreciated, good for new contributors
Milestone

Comments

@martimlobao
Copy link

martimlobao commented May 24, 2021

Steps to reproduce

Given a file a.py:

def main():
    mydict = {"foo": 1, "bar": 2}
    return [(k, v) for k, v in mydict.items()]

Pylint will complain about an unnecessary comprehension, even though the comprehension is modifying the type from a dictionary to a list of tuples.

Current behavior

Result of pylint a.py:

$ pylint a.py 
************* Module example
example.py:3:0: R1721: Unnecessary use of a comprehension (unnecessary-comprehension)

Expected behavior

No errors.

pylint --version output

Result of pylint --version output:

$ pylint --version output
pylint 2.8.2
astroid 2.5.6
Python 3.8.7 (default, Jan 22 2021, 22:54:27) 
[Clang 12.0.0 (clang-1200.0.32.28)]

EDIT: I guess alternatively it might be cleaner to use list(mydict.items()) instead. That feels like it might be a separate issue altogether, and I'm not sure if there aren't other false positives that can't be solved similarly. It's not that the comprehension is unnecessary, it's that there's a nicer way to do it. Maybe a new warning might also be a good idea?

@Pierre-Sassoulas Pierre-Sassoulas added Good first issue Friendly and approachable by new contributors False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors labels May 24, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue !

@cdce8p
Copy link
Member

cdce8p commented May 24, 2021

I think this is more a case of a misleading error message that could be improved. As you pointed out list(mydict.items()) is indeed equivalent and IMO the better solution. So technically this is an unnecessary-comprehension.

I can't remember the exact code, but I had something similar happen a while back where it took me more than a short moment to figure out a solution.

@cdce8p cdce8p added Documentation 📗 and removed False Positive 🦟 A message is emitted but nothing is wrong with the code labels May 24, 2021
@yushao2
Copy link
Collaborator

yushao2 commented May 24, 2021

Proposing an improvement to helptext for this checker in my PR, please take a look :)

@yushao2
Copy link
Collaborator

yushao2 commented May 24, 2021

@Pierre-Sassoulas is it possible to assign this to me since I've already started some work on it?

Thanks!

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

Successfully merging a pull request may close this issue.

4 participants