Repository navigation
[flake8-comprehensions] Document map/generator exception behavior (C417) - #27794
Conversation
There was a problem hiding this comment.
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 problemssection explaining the exception/iteration behavior difference betweenmap()and generator expressions. - Expand
## Fix safetyto 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
left a comment
There was a problem hiding this comment.
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.
flake8-comprehensions] Document map/generator exception behavior (C417)
|
Summary
Documents that
mapobjects and generator expressions are not interchangeablewhen 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 raisesStopIteration. Amapobject has no frame toclose, 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 problemssection describing the difference and notes it under
## Fix safetyas anadditional reason the fix is unsafe.
Docs-only; no behavior change.
Closes #27304
Test Plan
cargo dev generate-all, pluscargo nextest run -p ruff_linter -p ruff_devand
cargo clippy -p ruff_linter --all-targets -- -D warningsto confirm thedoc comment still builds and no generated artifact is stale.