MCPcopy Create free account
hub / github.com/astral-sh/ruff / unparse_expr

Method unparse_expr

crates/ruff_python_parser/src/parser/statement.rs:1128–1171  ·  view source on GitHub ↗

We are permissive than the original implementation because we would allow whitespace between the expression and the suffix while the IPython implementation doesn't allow it. For example, `foo ?` would be valid in our case but invalid for IPython.

(parser: &mut Parser, expr: &Expr, buffer: &mut String)

Source from the content-addressed store, hash-verified

1126 // between the expression and the suffix while the IPython implementation doesn't allow it.
1127 // For example, `foo ?` would be valid in our case but invalid for IPython.
1128 fn unparse_expr(parser: &mut Parser, expr: &Expr, buffer: &mut String) {
1129 match expr {
1130 Expr::Name(ast::ExprName { id, .. }) => {
1131 buffer.push_str(id.as_str());
1132 }
1133 Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
1134 unparse_expr(parser, value, buffer);
1135 buffer.push('[');
1136
1137 if let Expr::NumberLiteral(ast::ExprNumberLiteral {
1138 value: ast::Number::Int(integer),
1139 ..
1140 }) = &**slice
1141 {
1142 let _ = write!(buffer, "{integer}");
1143 } else {
1144 parser.add_error(
1145 ParseErrorType::OtherError(
1146 "Only integer literals are allowed in subscript expressions in help end escape command"
1147 .to_string()
1148 ),
1149 slice.range(),
1150 );
1151 buffer.push_str(parser.src_text(slice.range()));
1152 }
1153
1154 buffer.push(']');
1155 }
1156 Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
1157 unparse_expr(parser, value, buffer);
1158 buffer.push('.');
1159 buffer.push_str(attr.as_str());
1160 }
1161 _ => {
1162 parser.add_error(
1163 ParseErrorType::OtherError(
1164 "Expected name, subscript or attribute expression in help end escape command"
1165 .to_string()
1166 ),
1167 expr,
1168 );
1169 }
1170 }
1171 }
1172
1173 let start = self.node_start();
1174 self.bump(TokenKind::Question);

Callers

nothing calls this directly

Calls 7

src_textMethod · 0.80
push_strMethod · 0.45
as_strMethod · 0.45
pushMethod · 0.45
add_errorMethod · 0.45
to_stringMethod · 0.45
rangeMethod · 0.45

Tested by

no test coverage detected