gh-121130: Fix f-string format specifiers with debug expressions (#121150)

This commit is contained in:
Pablo Galindo Salgado 2024-07-16 19:57:22 +01:00 committed by GitHub
parent 69c68de43a
commit c46d64e0ef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 75 additions and 31 deletions

View file

@ -8,6 +8,7 @@
# Unicode identifiers in tests is allowed by PEP 3131.
import ast
import datetime
import dis
import os
import re
@ -1601,6 +1602,12 @@ x = (
self.assertEqual(f'{f(a=4)}', '3=')
self.assertEqual(x, 4)
# Check debug expressions in format spec
y = 20
self.assertEqual(f"{2:{y=}}", "yyyyyyyyyyyyyyyyyyy2")
self.assertEqual(f"{datetime.datetime.now():h1{y=}h2{y=}h3{y=}}",
'h1y=20h2y=20h3y=20')
# Make sure __format__ is being called.
class C:
def __format__(self, s):
@ -1614,9 +1621,11 @@ x = (
self.assertEqual(f'{C()=: }', 'C()=FORMAT- ')
self.assertEqual(f'{C()=:x}', 'C()=FORMAT-x')
self.assertEqual(f'{C()=!r:*^20}', 'C()=********REPR********')
self.assertEqual(f"{C():{20=}}", 'FORMAT-20=20')
self.assertRaises(SyntaxError, eval, "f'{C=]'")
# Make sure leading and following text works.
x = 'foo'
self.assertEqual(f'X{x=}Y', 'Xx='+repr(x)+'Y')