Skip to content
New issue

Have a question about this project? 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

Annotated assignment in a match block does not generate SETUP_ANNOTATIONS #105164

Closed
martindemello opened this issue May 31, 2023 · 3 comments
Closed
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) triaged The issue has been accepted as valid by a triager. type-bug An unexpected behavior, bug, or error

Comments

@martindemello
Copy link
Contributor

martindemello commented May 31, 2023

Bug report

Compiling the following code:

match 0:
    case 0:
        x: int = 1

generates

  1           0 LOAD_CONST               0 (0)

  2           2 LOAD_CONST               0 (0)
              4 COMPARE_OP               2 (==)
              6 POP_JUMP_IF_FALSE       12 (to 24)

  3           8 LOAD_CONST               1 (1)
             10 STORE_NAME               0 (x)
             12 LOAD_NAME                1 (int)
             14 LOAD_NAME                2 (__annotations__)
             16 LOAD_CONST               2 ('x')
             18 STORE_SUBSCR
             20 LOAD_CONST               3 (None)
             22 RETURN_VALUE

  2     >>   24 LOAD_CONST               3 (None)
             26 RETURN_VALUE

There is no SETUP_ANNOTATIONS opcode generated despite the __annotations__ access later on. (Found via google/pytype#1435 - for some reason this does not generate a runtime error, but it does cause issues for tools like pytype.)

Your environment

cpython 3.10

Linked PRs

@martindemello martindemello added the type-bug An unexpected behavior, bug, or error label May 31, 2023
@JelleZijlstra JelleZijlstra self-assigned this May 31, 2023
@yilei
Copy link
Contributor

yilei commented May 31, 2023

FWIW, it's a runtime error when the module is imported, not run as main:

$ cat lib.py
match 0:
    case 0:
        x: int = 1

$ cat main.py
import lib

$ python main.py
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import lib
  File "lib.py", line 3, in <module>
    x: int = 1
NameError: name '__annotations__' is not defined

@brandtbucher
Copy link
Member

brandtbucher commented May 31, 2023

I think find_ann in compile.c needs to be taught about Match_kind.

Looks like @JelleZijlstra picked this up already. Feel free to ping me for review!

@brandtbucher brandtbucher added 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) triaged The issue has been accepted as valid by a triager. labels May 31, 2023
@brandtbucher brandtbucher changed the title annotated assignment in a match block does not generate SETUP_ANNOTATIONS Annotated assignment in a match block does not generate SETUP_ANNOTATIONS May 31, 2023
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Jun 1, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 5, 2023
)

(cherry picked from commit 69d1245)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Jun 5, 2023
…nGH-105177).

(cherry picked from commit 69d1245)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit that referenced this issue Jun 5, 2023
…105313)

(cherry picked from commit 69d1245)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@hauntsaninja
Copy link
Contributor

Thanks, looks like this is fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) triaged The issue has been accepted as valid by a triager. type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants