mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:41:23 +00:00
Simplify quote annotator logic for list expression (#14425)
## Summary Follow-up to #14371, this PR simplifies the visitor logic for list expressions to remove the state management. We just need to make sure that we visit the nested expressions using the `QuoteAnnotator` and not the `Generator`. This is similar to what's being done for binary expressions. As per the [grammar](https://typing.readthedocs.io/en/latest/spec/annotations.html#grammar-token-expression-grammar-annotation_expression), list expressions can be present which can contain other type expressions (`Callable`): ``` | <Callable> '[' <Concatenate> '[' (type_expression ',')+ (name | '...') ']' ',' type_expression ']' (where name must be a valid in-scope ParamSpec) | <Callable> '[' '[' maybe_unpacked (',' maybe_unpacked)* ']' ',' type_expression ']' ``` ## Test Plan `cargo insta test`
This commit is contained in:
parent
cd2ae5aa2d
commit
38a385fb6f
1 changed files with 9 additions and 11 deletions
|
@ -392,23 +392,21 @@ impl<'a> SourceOrderVisitor<'a> for QuoteAnnotator<'a> {
|
||||||
}
|
}
|
||||||
self.state.pop();
|
self.state.pop();
|
||||||
}
|
}
|
||||||
|
// For the following expressions, we just need to make sure to visit the nested
|
||||||
|
// expressions using the quote annotator and not use the generator. This is so that any
|
||||||
|
// subscript elements nested within them are identified and quoted correctly.
|
||||||
Expr::List(ast::ExprList { elts, .. }) => {
|
Expr::List(ast::ExprList { elts, .. }) => {
|
||||||
let Some((first, remaining)) = elts.split_first() else {
|
let mut first = true;
|
||||||
return;
|
|
||||||
};
|
|
||||||
self.annotation.push('[');
|
self.annotation.push('[');
|
||||||
self.visit_expr(first);
|
for expr in elts {
|
||||||
if let Some(last) = self.state.last_mut() {
|
if first {
|
||||||
if *last == QuoteAnnotatorState::AnnotatedFirst {
|
first = false;
|
||||||
*last = QuoteAnnotatorState::AnnotatedRest;
|
} else {
|
||||||
}
|
|
||||||
}
|
|
||||||
for expr in remaining {
|
|
||||||
self.annotation.push_str(", ");
|
self.annotation.push_str(", ");
|
||||||
|
}
|
||||||
self.visit_expr(expr);
|
self.visit_expr(expr);
|
||||||
}
|
}
|
||||||
self.annotation.push(']');
|
self.annotation.push(']');
|
||||||
self.state.pop();
|
|
||||||
}
|
}
|
||||||
Expr::BinOp(ast::ExprBinOp {
|
Expr::BinOp(ast::ExprBinOp {
|
||||||
left, op, right, ..
|
left, op, right, ..
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue