Skip to content

[flake8-comprehensions] Document map/generator exception behavior (C417) - #27794

Merged
ntBre merged 2 commits into
astral-sh:mainfrom
sanjayrohith:docs/c417-map-generator-exceptions
Sep 24, 2026
Merged

ntBre merged 2 commits into
astral-sh:mainfrom
sanjayrohith:docs/c417-map-generator-exceptions

Conversation

@sanjayrohith

Copy link
Copy Markdown
Contributor

Summary

Documents that map objects and generator expressions are not interchangeable
when the mapped expression raises, which is a known source of false positives
for unnecessary-map (C417).

Once an exception propagates out of a generator, the generator is closed and
every later next() call raises StopIteration. A map object has no frame to
close, so a caller that handles the exception can keep iterating. Code that
relies on this - for example, collecting every element that converts
successfully and reporting all failures at the end, rather than stopping at the
first one - behaves differently after C417's fix is applied.

As discussed in the issue, there isn't a good way for Ruff to detect this
situation and suppress the diagnostic, so this adds a ## Known problems
section describing the difference and notes it under ## Fix safety as an
additional reason the fix is unsafe.

Docs-only; no behavior change.

Closes #27304

Test Plan

cargo dev generate-all, plus cargo nextest run -p ruff_linter -p ruff_dev
and cargo clippy -p ruff_linter --all-targets -- -D warnings to confirm the
doc comment still builds and no generated artifact is stale.

Copilot AI lite review requested due to automatic review settings August 16, 2026 06:26
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre August 16, 2026 06:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Rustdoc for the flake8-comprehensions C417 rule (unnecessary-map) to document a known semantic difference between map() objects and generator expressions when the mapping operation raises, clarifying why Ruff cannot reliably suppress false positives and why the fix is marked unsafe.

Changes:

  • Add a ## Known problems section explaining the exception/iteration behavior difference between map() and generator expressions.
  • Expand ## Fix safety to note that the fix can change error-propagation/iterator behavior in addition to occasionally dropping comments.

πŸ’‘ Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this looks good to me. Initially I thought the example was a bit long, but I think this is a pretty subtle issue that justifies an example. I just had one nit about the fix safety docs.

@ntBre ntBre added the documentation Improvements or additions to public-facing documentation label Aug 17, 2026
@ntBre ntBre changed the title [flake8-comprehensions] Document map/generator exception behavior (C417) [flake8-comprehensions] Document map/generator exception behavior (C417) Aug 17, 2026
@astral-sh-bot

astral-sh-bot Bot commented Sep 24, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

βœ… ecosystem check detected no linter changes.

Linter (preview)

βœ… ecosystem check detected no linter changes.

@ntBre
ntBre merged commit 8c244e5 into astral-sh:main Sep 24, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to public-facing documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C417 - map and generator expressions are not equivalent

3 participants