Remove unnecessary map usage using a list comprehension #795
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We can postprocess generation with ruff check like:
MAVSDK-Python/other/tools/run_protoc.sh
Line 102 in 5717d9e
Or we could have done that rewriting in:
%
ruff check --select=C417 --fix --unsafe-fixes%
ruff rule C417unnecessary-map (C417)
Derived from the flake8-comprehensions linter.
Fix is sometimes available.
What it does
Checks for unnecessary
map()calls with lambda functions.Why is this bad?
Using
map(func, iterable)whenfuncis a lambda is slower thanusing a generator expression or a comprehension, as the latter approach
avoids the function call overhead, in addition to being more readable.
This rule also applies to
map()calls withinlist(),set(), anddict()calls. For example:list(map(lambda num: num * 2, nums)), use[num * 2 for num in nums].set(map(lambda num: num % 2 == 0, nums)), use{num % 2 == 0 for num in nums}.dict(map(lambda v: (v, v ** 2), values)), use{v: v ** 2 for v in values}.Example
Use instead:
Fix safety
This rule's fix is marked as unsafe, as it may occasionally drop comments
when rewriting the call. In most cases, though, comments will be preserved.