@@ -661,15 +661,50 @@ def test_comments(self):
661
661
self .assertEqual (f'{ "#" } ' , '#' )
662
662
self .assertEqual (f'{ d ["#" ]} ' , 'hash' )
663
663
664
- self .assertAllRaise (SyntaxError , "f-string expression part cannot include '#'" ,
665
- ["f'{1#}'" , # error because the expression becomes "(1#)"
666
- "f'{3(#)}'" ,
664
+ self .assertAllRaise (SyntaxError , "'{' was never closed" ,
665
+ ["f'{1#}'" , # error because everything after '#' is a comment
667
666
"f'{#}'" ,
667
+ "f'one: {1#}'" ,
668
+ "f'{1# one} {2 this is a comment still#}'" ,
668
669
])
669
670
self .assertAllRaise (SyntaxError , r"f-string: unmatched '\)'" ,
670
671
["f'{)#}'" , # When wrapped in parens, this becomes
671
672
# '()#)'. Make sure that doesn't compile.
672
673
])
674
+ self .assertEqual (f'''A complex trick: {
675
+ 2 # two
676
+ } ''' , 'A complex trick: 2' )
677
+ self .assertEqual (f'''
678
+ {
679
+ 40 # fourty
680
+ + # plus
681
+ 2 # two
682
+ } ''' , '\n 42' )
683
+ self .assertEqual (f'''
684
+ {
685
+ 40 # fourty
686
+ + # plus
687
+ 2 # two
688
+ } ''' , '\n 42' )
689
+
690
+ self .assertEqual (f'''
691
+ # this is not a comment
692
+ { # the following operation it's
693
+ 3 # this is a number
694
+ * 2 } ''' , '\n # this is not a comment\n 6' )
695
+ self .assertEqual (f'''
696
+ { # f'a {comment}'
697
+ 86 # constant
698
+ # nothing more
699
+ } ''' , '\n 86' )
700
+
701
+ self .assertAllRaise (SyntaxError , r"f-string: valid expression required before '}'" ,
702
+ ["""f'''
703
+ {
704
+ # only a comment
705
+ }'''
706
+ """ , # this is equivalent to f'{}'
707
+ ])
673
708
674
709
def test_many_expressions (self ):
675
710
# Create a string with many expressions in it. Note that
0 commit comments