Skip to content

Commit

Permalink
allow normal dotted names
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Feb 26, 2025
1 parent 4b7b98c commit 91d9f95
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# parse_options: { "target_version": "3.8" }
@buttons.clicked.connect
def spam(): ...
25 changes: 22 additions & 3 deletions crates/ruff_python_parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2530,11 +2530,25 @@ impl<'src> Parser<'src> {
fn try_parse_old_decorators(&mut self) -> Option<ParsedExpr> {
let errors = self.errors.len();
let start = self.node_start();
let name = self.parse_name();
if name.ctx.is_invalid() {
// initial identifier
let ident = self.parse_identifier();
if !ident.is_valid() {
return None;
}
let name = Expr::from(name);
let mut name = Expr::from(ast::ExprName {
range: self.node_range(start),
id: ident.id,
ctx: ExprContext::Load,
});
// ("." identifier)*
while self.at(TokenKind::Dot) {
let attr = self.parse_attribute_expression(name, start);
if !attr.attr.is_valid() {
return None;
}
name = Expr::from(attr);
}
// ["(" [argument_list [","]] ")"] NEWLINE
let parsed = match self.current_token_kind() {
TokenKind::Lpar => Some(Expr::Call(self.parse_call_expression(name, start)).into()),
TokenKind::Newline => Some(name.into()),
Expand Down Expand Up @@ -2564,6 +2578,11 @@ impl<'src> Parser<'src> {
// before 3.9 but avoid false positives on examples like the `@_` "identity function hack"
// or the "eval hack" called out in the PEP.

// test_ok decorator_expression_dotted_ident_before_py39
// # parse_options: { "target_version": "3.8" }
// @buttons.clicked.connect
// def spam(): ...

// test_ok decorator_expression_identity_hack_before_py39
// # parse_options: { "target_version": "3.8" }
// def _(x): return x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/decorator_expression_dotted_ident_before_py39.py
---
## AST

```
Module(
ModModule {
range: 0..86,
body: [
FunctionDef(
StmtFunctionDef {
range: 45..85,
is_async: false,
decorator_list: [
Decorator {
range: 45..69,
expression: Attribute(
ExprAttribute {
range: 46..69,
value: Attribute(
ExprAttribute {
range: 46..61,
value: Name(
ExprName {
range: 46..53,
id: Name("buttons"),
ctx: Load,
},
),
attr: Identifier {
id: Name("clicked"),
range: 54..61,
},
ctx: Load,
},
),
attr: Identifier {
id: Name("connect"),
range: 62..69,
},
ctx: Load,
},
),
},
],
name: Identifier {
id: Name("spam"),
range: 74..78,
},
type_params: None,
parameters: Parameters {
range: 78..80,
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [
Expr(
StmtExpr {
range: 82..85,
value: EllipsisLiteral(
ExprEllipsisLiteral {
range: 82..85,
},
),
},
),
],
},
),
],
},
)
```

0 comments on commit 91d9f95

Please sign in to comment.