Visitar URL original
Comprehension inlining: Bug if comprehension contains a lambda · Issue #104357 · python/cpython · GitHub
Skip to content

Comprehension inlining: Bug if comprehension contains a lambda #104357

Description

@JelleZijlstra

Code sample:

def outer(x):
    return [lambda: x for x in range(x)]

print([f() for f in outer(2)])

On 3.11, this produces [1, 1] as expected.

But on current main, I get:

>>> [f() for f in outer(2)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in outer
TypeError: 'cell' object cannot be interpreted as an integer

Almost certainly due to PEP 709 / #101441, cc @carljm.

Linked PRs

Activity

  1. self-assigned this
    on May 10, 2023
  2. carljm commented on May 10, 2023

    @carljm
    Member

    Thanks for finding this case! I had tests for similar cases with lambda inside the comprehension using the same name as non-cell outside the comprehension, but they were all of the form:

    def outer():
        x = 2
        return [lambda: x for x in range(x)]
    

    The key factor in your example is that there is no store to x in the outer scope, because it is an argument.

  3. added a commit that references this issue on May 10, 2023
  4. added a commit that references this issue on May 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions