Skip to content

C417 fix introduces a syntax error for yield in lambda expression #20198

@dscorbett

Description

@dscorbett

Summary

The fix for unnecessary-map (C417) introduces a syntax error when the lambda expression contains a yield expression. The fix, and possibly the rule, should be suppressed in that case. Example:

$ cat >c417.py <<'# EOF'
base = 0
gens = map(lambda x: (yield base + x), [1, 2, 3])
for gen in gens:
    print(*[*gen])
    base += 20
# EOF

$ python c417.py
1
22
43

$ ruff --isolated check c417.py --select C417 --unsafe-fixes --fix
Found 1 error (1 fixed, 0 remaining).

$ python c417.py 2>&1 | tail -n 3
    gens = ((yield base + x) for x in [1, 2, 3])
             ^^^^^^^^^^^^^^
SyntaxError: 'yield' inside generator expression

Version

ruff 0.12.11 (c2bc15b 2025-08-28)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions