Martin Panter added the comment:

I think this patch should do it. No major surgery required, just a good dose of 
recursion :)

def test_nested_fstrings(self):  # Original code
    y = 5
    self.assertEqual(f'{f"{0}"*3}', '000')
    self.assertEqual(f'{f"{y}"*3}', '555')
    self.assertEqual(f'{f"{\'x\'}"*3}', 'xxx')

    self.assertEqual(f"{r'x' f'{\"s\"}'}", 'xs')
    self.assertEqual(f"{r'x'rf'{\"s\"}'}", 'xs')

def test_nested_fstrings(self):  # Unparsed output
    y = 5
    self.assertEqual(f"{(f'{0}' * 3)}", '000')
    self.assertEqual(f"{(f'{y}' * 3)}", '555')
    self.assertEqual(f'{(f"{\'x\'}" * 3)}', 'xxx')
    self.assertEqual(f'{f"x{\'s\'}"}', 'xs')
    self.assertEqual(f'{f"x{\'s\'}"}', 'xs')

There was no problem getting the quoting right; repr() takes care of that, and 
then you slap the “f” on the front. The most subtle thing was knowing that f"{ 
{...} }" cannot be unparsed as f"{{...}}". I unparse other expressions without 
adding spaces, but unparse that one as f"{ {...}}".

Tested by running ./python -bWall -m test -u cpu test_tools

----------
keywords: +patch
nosy: +martin.panter
stage: needs patch -> patch review
Added file: http://bugs.python.org/file40524/fstring-unparse.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25180>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to