Render @decorator syntax for function and class defs#612
Open
mariopenterman wants to merge 1 commit into
Open
Conversation
Reconstruct decorator application (PEP 318) for Python 3.11. A decorated def compiles to `LOAD <deco>; MAKE_FUNCTION; PRECALL 0; CALL 0`, leaving the function in the CALL "self" slot with argc 0; previously this was mis-rendered as an IIFE `name = (lambda ...)()`, silently dropping the decorator. - ASTFunction / ASTClass carry an ordered decorator list. - A no-arg CALL whose callable is a freshly built function/class is recognized as decorator application: the def is bound to its name and the decorator wraps that name. - When a store re-binds a def's name to `deco(name)`, the call chain is peeled and attached to the def so the printer emits real `@deco` lines. Function and method decorators (single, stacked, and dotted) now render as proper decorator syntax. Signed-off-by: Mario Penterman <mariopenterman@gmail.com>
a312972 to
7dd2415
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decorated function/class defs are currently mis-rendered as an IIFE, silently dropping the decorator and producing invalid Python.
Before (current
master)decompiles to:
After this PR
What it does
On 3.11 a decorated def compiles to
LOAD <deco>; MAKE_FUNCTION; PRECALL 0; CALL 0, leaving the function in the CALL "self" slot with argc 0. This recognizes that no-arg-CALL-of-a-freshly-built-def pattern as decorator application, attaches the call chain to the def, and emits real@decolines. Single, stacked, and dotted decorators are handled.A self-contained test (
tests/input/decorators.py+ compiled.pyc+ expected tokenized output) is included; the full suite stays green.