mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 17:41:12 +00:00
[syntax-errors] Named expressions in decorators before Python 3.9 (#16386)
Summary -- This PR detects the relaxed grammar for decorators proposed in [PEP 614](https://peps.python.org/pep-0614/) on Python 3.8 and lower. The 3.8 grammar for decorators is [here](https://docs.python.org/3.8/reference/compound_stmts.html#grammar-token-decorators): ``` decorators ::= decorator+ decorator ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE dotted_name ::= identifier ("." identifier)* ``` in contrast to the current grammar [here](https://docs.python.org/3/reference/compound_stmts.html#grammar-token-python-grammar-decorators) ``` decorators ::= decorator+ decorator ::= "@" assignment_expression NEWLINE assignment_expression ::= [identifier ":="] expression ``` Test Plan -- New inline parser tests.
This commit is contained in:
parent
d0623888b3
commit
318f503714
16 changed files with 876 additions and 3 deletions
|
@ -43,3 +43,15 @@ pub(super) const fn token_kind_to_cmp_op(tokens: [TokenKind; 2]) -> Option<CmpOp
|
|||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper for `parse_decorators` to determine if `expr` is a [`dotted_name`] from the decorator
|
||||
/// grammar before Python 3.9.
|
||||
///
|
||||
/// [`dotted_name`]: https://docs.python.org/3.8/reference/compound_stmts.html#grammar-token-dotted-name
|
||||
pub(super) fn is_name_or_attribute_expression(expr: &Expr) -> bool {
|
||||
match expr {
|
||||
Expr::Attribute(attr) => is_name_or_attribute_expression(&attr.value),
|
||||
Expr::Name(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue