Skip to content

Conversation

@cclauss
Copy link
Contributor

@cclauss cclauss commented Sep 10, 2025

We can postprocess generation with ruff check like:

Or we could have done that rewriting in:


% ruff check --select=C417 --fix --unsafe-fixes

Found 24 errors (24 fixed, 0 remaining).

% ruff rule C417

unnecessary-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) when func is a lambda is slower than
using 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 within list(), set(), and
dict() calls. For example:

  • Instead of list(map(lambda num: num * 2, nums)), use
    [num * 2 for num in nums].
  • Instead of set(map(lambda num: num % 2 == 0, nums)), use
    {num % 2 == 0 for num in nums}.
  • Instead of dict(map(lambda v: (v, v ** 2), values)), use
    {v: v ** 2 for v in values}.

Example

map(lambda x: x + 1, iterable)

Use instead:

(x + 1 for x in iterable)

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.

Copy link
Collaborator

@JonasVautherin JonasVautherin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again again (mentioned e.g. here), those files are autogenerated.

EDIT: apparently this is done in #796? Should we keep this one open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants