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

Method unparse_expr

crates/ruff_python_parser/src/parser/statement.rs:1140–1185  ·  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

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

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected