Skip to content

AST optimizer skips PEP 696 type parameter defaults #123344

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

Closed
JelleZijlstra opened this issue Aug 26, 2024 · 4 comments
Closed

AST optimizer skips PEP 696 type parameter defaults #123344

JelleZijlstra opened this issue Aug 26, 2024 · 4 comments
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Aug 26, 2024

Bug report

Bug description:

>>> dis.dis("def f[T = 1 + 2](): pass")
... snip ...
      L1:     LOAD_CONST               0 (1)
              LOAD_CONST               1 (2)
              BINARY_OP                0 (+)

... snip ...

cc @Eclips4. This is easy to fix but it might be better to wait for #122667 so we can test the fix better.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

@JelleZijlstra JelleZijlstra added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 bugs and security fixes labels Aug 26, 2024
@sobolevn
Copy link
Member

diff --git Python/ast_opt.c Python/ast_opt.c
index d7a26e64150..8907c6daec7 100644
--- Python/ast_opt.c
+++ Python/ast_opt.c
@@ -1087,6 +1087,7 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
     switch (node_->kind) {
         case TypeVar_kind:
             CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
+            CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value);
             break;
         case ParamSpec_kind:
             break;

This might help someone in the future, I tested it locally: echo 'def f[T = 1 + 2](): pass' | ./python.exe -m dis

LOAD_CONST               3 (<code object f at 0x1019cab60, file "<stdin>", line 1>)

@JelleZijlstra
Copy link
Member Author

The same change is also needed for the ParamSpec and TypeVarTuple cases though.

@Eclips4
Copy link
Member

Eclips4 commented Aug 26, 2024

If no one complains, I would like to work with @wrongnull on this

JelleZijlstra added a commit that referenced this issue Aug 28, 2024
Co-authored-by: Kirill Podoprigora <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 28, 2024
…123377)

(cherry picked from commit be083ce)

Co-authored-by: Bogdan Romanyuk <[email protected]>
Co-authored-by: Kirill Podoprigora <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
JelleZijlstra added a commit that referenced this issue Aug 28, 2024
… (#123427)


(cherry picked from commit be083ce)

Co-authored-by: Bogdan Romanyuk <[email protected]>
Co-authored-by: Kirill Podoprigora <[email protected]>
Co-authored-by: Jelle Zijlstra <[email protected]>
@picnixz

This comment was marked as resolved.

@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants