Skip to content

Preserve deferred class annotation registration gates - #23

Open
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/class-annotation-registration
Open

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/class-annotation-registration

Conversation

@EiNSTeiN-

Copy link
Copy Markdown

I am opening this PR to preserve the compiler's registration gates when reconstructing deferred class annotations. The code below currently loses the constant registration block when it is decompiled:

def select(enabled, values, default):
    class Result:
        if True:
            amount: int = 3
        if enabled:
            label: str = "ready"
        for value in values:
            limit: int = default(value)
    return Result

The first class entry is currently emitted as an unconditional annotation:

class Result:
    amount: int = 3
    # Remaining class statements follow.

That preserves the visible value but changes how the class registers its annotations. In a class containing only constant-folded registration blocks, the unconditional output also loses the __conditional_annotations__ closure cell. In a mixed class, it changes the annotation function's guarded entries.

Conditional class annotations are reconstructed from a deferred annotation function and registration markers. The change passes class-scope information into p649_restore_gates and retains a readable if True registration block for class entries. Consecutive registration blocks are combined, while each field retains its compiler gate. The restoration recurses through conditionals, loops and handlers, preserving whether each annotation is registered and when its initializer runs.

The retained wrappers preserve annotation registration and closure metadata but can introduce NOPs in the surrounding class code. Module entries retain their existing handling. Unreachable, unregistered entries remain omitted; the documented dead-branch STRING-format limitation remains.

The deferred evaluation and __annotate__ machinery addressed here begin in Python 3.14, as specified by PEP 649 and PEP 749. docs/LIMITATIONS.md now describes the preservation of live class registration gates.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant