Skip to content

Commit 8c244e5

Browse files
sanjayrohithntBre
andauthored
[flake8-comprehensions] Document map/generator exception behavior (C417) (#27794)
## 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. --------- Co-authored-by: Brent Westbrook <[email protected]>
1 parent 5edf5a1 commit 8c244e5

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

‎crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_map.rs‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,34 @@ use crate::{FixAvailability, Violation};
4242
/// (x + 1 for x in iterable)
4343
/// ```
4444
///
45+
/// ## Known problems
46+
/// A `map` object and a generator expression are not interchangeable when the
47+
/// mapped expression raises. Once an exception propagates out of a generator,
48+
/// the generator is closed, so every later `next()` call raises
49+
/// `StopIteration`. A `map` object is not closed, so iteration can resume after
50+
/// the error:
51+
///
52+
/// ```python
53+
/// values = ["0", "x", "2"]
54+
///
55+
/// m = map(lambda v: int(v), values)
56+
/// next(m) # 0
57+
/// next(m) # raises ValueError
58+
/// next(m) # 2
59+
///
60+
/// g = (int(v) for v in values)
61+
/// next(g) # 0
62+
/// next(g) # raises ValueError
63+
/// next(g) # raises StopIteration
64+
/// ```
65+
///
66+
/// Ruff cannot tell whether a caller relies on this difference, so the
67+
/// diagnostic is still reported in such cases.
68+
///
4569
/// ## Fix safety
46-
/// This rule's fix is marked as unsafe, as it may occasionally drop comments
47-
/// when rewriting the call. In most cases, though, comments will be preserved.
70+
/// This rule's fix is marked as unsafe because it can change how errors propagate
71+
/// out of the resulting iterator, as described above. It may also drop comments
72+
/// when rewriting the call.
4873
#[derive(ViolationMetadata)]
4974
#[violation_metadata(stable_since = "v0.0.74", category = Category::Complexity)]
5075
pub(crate) struct UnnecessaryMap {

0 commit comments

Comments
 (0)