Skip to content

Local annotation turns local variables in cells #130924

Closed

Description

@markshannon

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

Activity

  1. self-assigned this
    on Mar 6, 2025
  2. gvanrossum commented on Mar 6, 2025

    @gvanrossum
    Member

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

  3. JelleZijlstra commented on Mar 6, 2025

    @JelleZijlstra
    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.

  4. sergey-miryanov commented on Mar 21, 2025

    @sergey-miryanov
    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.

  5. added a commit that references this issue on Mar 28, 2025
  6. added a commit that references this issue on Mar 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions