Skip to content

Commit dba7421

Browse files
committed
Address review findings.
1 parent c3a8f12 commit dba7421

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

Lib/test/test_fstring.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,8 @@ def test_debug_conversion(self):
10931093
# Make sure nested fstrings still work.
10941094
self.assertEqual(f'{f"{3.1415=:.1f}":*^20}', '*****3.1415=3.1*****')
10951095

1096-
# Make sure text before and after !d works correctly.
1096+
# Make sure text before and after an expression with = works
1097+
# correctly.
10971098
pi = 'π'
10981099
self.assertEqual(f'alpha α {pi=} ω omega', "alpha α pi='π' ω omega")
10991100

@@ -1117,7 +1118,7 @@ def test_debug_conversion(self):
11171118

11181119
x = 20
11191120
# This isn't an assignment expression, it's 'x', with a format
1120-
# spec of '=10'.
1121+
# spec of '=10'. See test_walrus: you need to use parens.
11211122
self.assertEqual(f'{x:=10}', ' 20')
11221123

11231124
# Test named function parameters, to make sure '=' parsing works
@@ -1147,6 +1148,15 @@ def __repr__(self):
11471148
self.assertEqual(f'{C()=:x}', 'C()=FORMAT-x')
11481149
self.assertEqual(f'{C()=!r:*^20}', 'C()=********REPR********')
11491150

1151+
def test_walrus(self):
1152+
x = 20
1153+
# This isn't an assignment expression, it's 'x', with a format
1154+
# spec of '=10'. See test_walrus: you need to use parens.
1155+
self.assertEqual(f'{x:=10}', ' 20')
1156+
1157+
# This is an assignment expression, which requires parens.
1158+
self.assertEqual(f'{(x:=10)}', '10')
1159+
self.assertEqual(x, 10)
11501160

11511161

11521162
if __name__ == '__main__':
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Add a ``=`` conversion feature to f-strings. This can precede ``!s``,
1+
Add a ``=`` feature f-strings for debugging. This can precede ``!s``,
22
``!r``, or ``!a``. It produces the text of the expression, followed by
33
an equal sign, followed by the repr of the value of the expression. So
44
``f'{3*9+15=}'`` would be equal to the string ``'3*9+15=42'``. If
5-
``=`` is specified, the default conversion is set to ``!r``. Added a
6-
``!f`` conversion to switch back to the previous format behavior, if
7-
needed.
5+
``=`` is specified, the default conversion is set to ``!r``, unless a
6+
format spec is given, in which case the formatting behavior is
7+
unchanged, and __format__ will be used.

Python/ast.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5020,7 +5020,7 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
50205020
int conversion = -1; /* The conversion char. Use default if not
50215021
specified, or !r if using = and no format
50225022
spec. */
5023-
int equal_conversion = 0; /* Are we using the = conversion? */
5023+
int equal_conversion = 0; /* Are we using the = feature? */
50245024
PyObject *expr_text = NULL; /* The text of the expression, used for =. */
50255025
const char *expr_text_end;
50265026

@@ -5194,7 +5194,8 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
51945194
if (!simple_expression)
51955195
goto error;
51965196

5197-
/* Check for = conversion. */
5197+
/* Check for =, which puts the text value of the expression in
5198+
expr_text. */
51985199
if (**str == '=') {
51995200
*str += 1;
52005201
equal_conversion = 1;

Python/ast_unparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec)
656656
Py_DECREF(temp_fv_str);
657657

658658
if (e->v.FormattedValue.expr_text) {
659-
/* Use the = debug conversion. */
659+
/* Use the = for debug text expansion. */
660660
APPEND_STR("=");
661661
}
662662

0 commit comments

Comments
 (0)