-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add C417 - Unnecessary use of map
#409
Conversation
Can I also add set comprehensions for |
I think it makes sense - and I haven't found time to review this properly but it looks good. |
@adamchainz Merged it all into C417, and added set and dict as well. |
What is the status on this one? |
README.rst
Outdated
``map(f, iterable)`` has great performance when ``f`` is a built-in function, | ||
and it makes sense if your function already has a name. But if you need to | ||
introduce a lambda, it's more readable and faster to use a generator expression | ||
or a comprehension -- no function calls needed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tweaking the wording, most notably to remove the readability claim - people love to debate what's readable, but we can be absolutely sure that map is slower 😅
tests/test_flake8_comprehensions.py
Outdated
def test_C417_fail_1(flake8_path): | ||
(flake8_path / "example.py").write_text( | ||
dedent( | ||
"""\ | ||
map(lambda x: x * 2, iterable) | ||
list(map(lambda x: x * 2, iterable)) | ||
set(map(lambda num: num % 2 == 0, nums)) | ||
dict(map(lambda v: (v, v ** 2), values)) | ||
""" | ||
) | ||
) | ||
result = flake8_path.run_flake8() | ||
assert result.out_lines == [ | ||
"./example.py:1:1: C417 Unnecessary use of map - " | ||
"use a generator expression instead.", | ||
"./example.py:2:1: C417 Unnecessary use of map - " | ||
"use a list comprehension instead.", | ||
"./example.py:3:1: C417 Unnecessary use of map - " | ||
"use a set comprehension instead.", | ||
"./example.py:4:1: C417 Unnecessary use of map - " | ||
"use a dict comprehension instead.", | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will split this test - I deliberately want separate test cases so it's easy to see what fails/passes whilst editing code.
Released in 3.10.0, thanks! |
Resolves #405