Skip to content

Commit 1af9cc0

Browse files
Merge pull request #489 from OddCoincidence/decorator-paths
Allow attributes to be used as decorators
2 parents cb2a807 + 114d9df commit 1af9cc0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

parser/src/python.lalrpop

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,17 +613,26 @@ ClassDef: ast::LocatedStatement = {
613613
},
614614
};
615615

616+
Path: ast::Expression = {
617+
<n:Identifier> => ast::Expression::Identifier { name: n },
618+
<p:Path> "." <n:name> => {
619+
ast::Expression::Attribute {
620+
value: Box::new(p),
621+
name: n,
622+
}
623+
},
624+
};
625+
616626
// Decorators:
617627
Decorator: ast::Expression = {
618-
"@" <n:DottedName> <a: ("(" ArgumentList ")")?> "\n" => {
619-
let name = ast::Expression::Identifier { name: n };
628+
"@" <p:Path> <a: ("(" ArgumentList ")")?> "\n" => {
620629
match a {
621630
Some((_, args, _)) => ast::Expression::Call {
622-
function: Box::new(name),
631+
function: Box::new(p),
623632
args: args.0,
624633
keywords: args.1,
625634
},
626-
None => name,
635+
None => p,
627636
}
628637
},
629638
};

tests/snippets/decorators.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ def add(a, b):
1414

1515
assert c == 14
1616

17+
18+
def f(func): return lambda: 42
19+
class A: pass
20+
a = A()
21+
a.a = A()
22+
a.a.x = f
23+
24+
@a.a.x
25+
def func():
26+
pass
27+
28+
assert func() == 42

0 commit comments

Comments
 (0)