Repository navigation
Conversation
A decorator with a dotted name, e.g. a.b.c(x), was parsed into a single Name whose Id was the whole string "a.b.c", so it failed at runtime with NameError: name 'a.b.c' is not defined. Build the decorator expression as CPython 3.4 does in ast_for_dotted_name: a Name for the first component and one Attribute (Load) per further component, all at the start of the dotted name, wrapped in a Call when arguments are given. Plain decorators now also carry the name's position rather than the '@', as in CPython. The new dottedName helper lives in parser.go so that grammar.y keeps its line numbers: the regenerated y.go changes by one line and y.output does not change. Enable test_dotted and test_eval_order in vm/tests/decorators.py and expect AttributeError for the nullval.attr decorator, as in CPython's test_decorators.py. Add dotted decorator cases to the parser tests.
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.
A decorator with a dotted name was parsed into a single
NamewhoseIdis the whole dotted string, so@decorators.author('Cleese')fails at runtime withNameError: name 'decorators.author' is not defined.CPython 3.4 (
ast_for_decorator/ast_for_dotted_namein Python/ast.c) buildsName(a)plus oneAttribute(..., Load)per further component, all positioned at the start of the dotted name, and wraps that in aCallwhen there are arguments. Thedecoratorrule now builds the sameName/Attributechain through a smalldottedNamehelper. Plain decorators also get the name's position instead of the@(col 1 for@foo), as in CPython.The helper is in parser.go rather than the grammar.y prologue so grammar.y keeps its line numbers: the regenerated y.go changes by one line and y.output not at all.
Tests:
test_dottedandtest_eval_orderfrom CPython's test_decorators.py (both were commented out), and thenullval.attrcase now expectsAttributeError, as in CPython. All three fail on main withNameError.@a.staying a SyntaxError). No python3.4 here, so the new rows were written by hand in the generator's output format and cross-checked against CPython'sast.go test -race ./...passes on Go 1.18, 1.19 and 1.27.Fixes #264.