8000 Local annotation turns local variables in cells · Issue #130924 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Local annotation turns local variables in cells #130924

New issue

Have a question about this project 8000 ? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
markshannon opened this issue Mar 6, 2025 · 3 comments · Fixed by #131843
Closed

Local annotation turns local variables in cells #130924

markshannon opened this issue Mar 6, 2025 · 3 comments · Fixed by #131843
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@markshannon
Copy link
Member
markshannon commented Mar 6, 2025

Bug report

Bug description:

Compiling this function

def f(x):
    a:x
    return x

Gives this bytecode:

  --           MAKE_CELL                0 (x)

   1           RESUME                   0

   3           LOAD_DEREF               0 (x)
               RETURN_VALUE

Local variable annotations are supposed to be ignored by Python (they are used by static type checkers only),
so the addition of the annotation should not change the generated code.
The expected disassembly:

   1           RESUME                   0

   3           LOAD_LAST                0 (x)
               RETURN_VALUE

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@markshannon markshannon added the type-bug An unexpected behavior, bug, or error label Mar 6, 2025
@JelleZijlstra JelleZijlstra self-assigned this Mar 6, 2025
@gvanrossum
Copy link
Member

Maybe @JelleZijlstra can look into this -- it smells like an unanticipated effect of PEP 749?

@JelleZijlstra
Copy link
Member

Yes, I already assigned it to myself to look into.

I think this is harmless in the sense that it doesn't change any behavior we should guarantee, but it's a pessimization.

@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 7, 2025
@sergey-miryanov
Copy link
Contributor

I can confirm that this is related to PEP-749.
a:x introduces a child scope for __annotate__ and x is a free variable for that scope and this is poisons x for the whole function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0