Skip to content

parser: fix dotted decorator names - #265

Open
drew-512 wants to merge 1 commit into
go-python:mainfrom
drew-512:parser-dotted-decorator
Open

drew-512 wants to merge 1 commit into
go-python:mainfrom
drew-512:parser-dotted-decorator

Conversation

@drew-512

@drew-512 drew-512 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

A decorator with a dotted name was parsed into a single Name whose Id is the whole dotted string, so @decorators.author('Cleese') fails at runtime with NameError: name 'decorators.author' is not defined.

CPython 3.4 (ast_for_decorator / ast_for_dotted_name in Python/ast.c) builds Name(a) plus one Attribute(..., Load) per further component, all positioned at the start of the dotted name, and wraps that in a Call when there are arguments. The decorator rule now builds the same Name / Attribute chain through a small dottedName helper. 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:

  • vm/tests/decorators.py: enabled test_dotted and test_eval_order from CPython's test_decorators.py (both were commented out), and the nullval.attr case now expects AttributeError, as in CPython. All three fail on main with NameError.
  • parser: added dotted decorator cases to make_grammar_test.py and grammar_data_test.go (on a function, with call args, stacked on a class, and @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's ast.
  • go test -race ./... passes on Go 1.18, 1.19 and 1.27.

Fixes #264.

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.
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.

parser: dotted decorator names raise NameError

1 participant