Skip to content

AST optimizer skips PEP 696 type parameter defaults #123344

Closed

Description

@JelleZijlstra

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    3.13only security fixes
    3.14bugs and security fixes
    on Aug 26, 2024
  2. sobolevn commented on Aug 26, 2024

    @sobolevn
    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>)
    
  3. JelleZijlstra commented on Aug 26, 2024

    @JelleZijlstra
    MemberAuthor

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

  4. Eclips4 commented on Aug 26, 2024

    @Eclips4
    Member

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

  5. added a commit that references this issue on Aug 28, 2024
  6. added a commit that references this issue on Aug 28, 2024
  7. added a commit that references this issue on Aug 28, 2024
  8. picnixz commented on Aug 28, 2024

    @picnixz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixesinterpreter-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