@@ -31,7 +31,7 @@ impl<'a> FStringParser<'a> {
3131 let mut spec = None ;
3232 let mut delims = Vec :: new ( ) ;
3333 let mut conversion = ConversionFlag :: None ;
34- let mut pred_expression_text = String :: new ( ) ;
34+ let mut self_documenting = false ;
3535 let mut trailing_seq = String :: new ( ) ;
3636
3737 while let Some ( ch) = self . chars . next ( ) {
@@ -87,7 +87,7 @@ impl<'a> FStringParser<'a> {
8787 // match a python 3.8 self documenting expression
8888 // format '{' PYTHON_EXPRESSION '=' FORMAT_SPECIFIER? '}'
8989 '=' if self . chars . peek ( ) != Some ( & '=' ) && delims. is_empty ( ) => {
90- pred_expression_text = expression . to_string ( ) ; // safe expression before = to print it
90+ self_documenting = true ;
9191 }
9292
9393 ':' if delims. is_empty ( ) => {
@@ -182,7 +182,7 @@ impl<'a> FStringParser<'a> {
182182 if expression. is_empty ( ) {
183183 return Err ( EmptyExpression ) ;
184184 }
185- let ret = if pred_expression_text . is_empty ( ) {
185+ let ret = if !self_documenting {
186186 vec ! [ self . expr( ExprKind :: FormattedValue {
187187 value: Box :: new(
188188 parse_fstring_expr( & expression)
@@ -194,7 +194,7 @@ impl<'a> FStringParser<'a> {
194194 } else {
195195 vec ! [
196196 self . expr( ExprKind :: Constant {
197- value: Constant :: Str ( pred_expression_text + "=" ) ,
197+ value: Constant :: Str ( expression . clone ( ) + "=" ) ,
198198 kind: None ,
199199 } ) ,
200200 self . expr( ExprKind :: Constant {
@@ -206,7 +206,12 @@ impl<'a> FStringParser<'a> {
206206 parse_fstring_expr( & expression)
207207 . map_err( |e| InvalidExpression ( Box :: new( e. error) ) ) ?,
208208 ) ,
209- conversion: conversion as _,
209+ conversion: ( if conversion == ConversionFlag :: None && spec. is_none( )
210+ {
211+ ConversionFlag :: Repr
212+ } else {
213+ conversion
214+ } ) as _,
210215 format_spec: spec,
211216 } ) ,
212217 ]
@@ -222,10 +227,13 @@ impl<'a> FStringParser<'a> {
222227 }
223228 }
224229 }
225- ' ' if !pred_expression_text . is_empty ( ) => {
230+ ' ' if self_documenting => {
226231 trailing_seq. push ( ch) ;
227232 }
228233 _ => {
234+ if self_documenting {
235+ return Err ( ExpectedRbrace ) ;
236+ }
229237 expression. push ( ch) ;
230238 }
231239 }
0 commit comments