summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorCristián Maureira-Fredes <cmaureir@users.noreply.github.com>2023-05-22 10:30:07 (GMT)
committerGitHub <noreply@github.com>2023-05-22 10:30:07 (GMT)
commit0a7796052acb9cec8b13f8d0a5f304f56f26ec5b (patch)
tree2e38c6f8e4b9a307382973b6a022285d14cc707d /Lib/test
parent9bc80dac47f6d43d0bbfbf10c4cc3848b175e97f (diff)
downloadcpython-0a7796052acb9cec8b13f8d0a5f304f56f26ec5b.zip
cpython-0a7796052acb9cec8b13f8d0a5f304f56f26ec5b.tar.gz
cpython-0a7796052acb9cec8b13f8d0a5f304f56f26ec5b.tar.bz2
gh-102856: Allow comments inside multi-line f-string expresions (#104006)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_fstring.py41
1 files changed, 38 insertions, 3 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index fcb12d2..3ba2f94 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -661,15 +661,50 @@ x = (
self.assertEqual(f'{"#"}', '#')
self.assertEqual(f'{d["#"]}', 'hash')
- self.assertAllRaise(SyntaxError, "f-string expression part cannot include '#'",
- ["f'{1#}'", # error because the expression becomes "(1#)"
- "f'{3(#)}'",
+ self.assertAllRaise(SyntaxError, "'{' was never closed",
+ ["f'{1#}'", # error because everything after '#' is a comment
"f'{#}'",
+ "f'one: {1#}'",
+ "f'{1# one} {2 this is a comment still#}'",
])
self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'",
["f'{)#}'", # When wrapped in parens, this becomes
# '()#)'. Make sure that doesn't compile.
])
+ self.assertEqual(f'''A complex trick: {
+2 # two
+}''', 'A complex trick: 2')
+ self.assertEqual(f'''
+{
+40 # fourty
++ # plus
+2 # two
+}''', '\n42')
+ self.assertEqual(f'''
+{
+40 # fourty
++ # plus
+2 # two
+}''', '\n42')
+
+ self.assertEqual(f'''
+# this is not a comment
+{ # the following operation it's
+3 # this is a number
+* 2}''', '\n# this is not a comment\n6')
+ self.assertEqual(f'''
+{# f'a {comment}'
+86 # constant
+# nothing more
+}''', '\n86')
+
+ self.assertAllRaise(SyntaxError, r"f-string: valid expression required before '}'",
+ ["""f'''
+{
+# only a comment
+}'''
+""", # this is equivalent to f'{}'
+ ])
def test_many_expressions(self):
# Create a string with many expressions in it. Note that