@@ -367,26 +367,39 @@ def test_ast_structure(self):
367367 self .assertIsInstance (tree .body [0 ].value .values [0 ], ast .Constant )
368368 self .assertIsInstance (tree .body [0 ].value .values [1 ], ast .Interpolation )
369369
370- def test_error_conditions (self ):
371- # Test syntax errors
372- with self .assertRaisesRegex (SyntaxError , "'{' was never closed" ):
373- eval ("t'{" )
374-
375- with self .assertRaisesRegex (SyntaxError , "t-string: expecting '}'" ):
376- eval ("t'{a'" )
377-
378- with self .assertRaisesRegex (SyntaxError , "t-string: single '}' is not allowed" ):
379- eval ("t'}'" )
380-
370+ def test_syntax_errors (self ):
371+ for case , err in (
372+ ("t'" , "unterminated t-string literal" ),
373+ ("t'''" , "unterminated triple-quoted t-string literal" ),
374+ ("t''''" , "unterminated triple-quoted t-string literal" ),
375+ ("t'{" , "'{' was never closed" ),
376+ ("t'{'" , "t-string: expecting '}'" ),
377+ ("t'{a'" , "t-string: expecting '}'" ),
378+ ("t'}'" , "t-string: single '}' is not allowed" ),
379+ ("t'{}'" , "t-string: valid expression required before '}'" ),
380+ ("t'{=x}'" , "t-string: valid expression required before '='" ),
381+ ("t'{!x}'" , "t-string: valid expression required before '!'" ),
382+ ("t'{:x}'" , "t-string: valid expression required before ':'" ),
383+ ("t'{x;y}'" , "t-string: expecting '=', or '!', or ':', or '}'" ),
384+ ("t'{x=y}'" , "t-string: expecting '!', or ':', or '}'" ),
385+ ("t'{x!s!}'" , "t-string: expecting ':' or '}'" ),
386+ ("t'{x!s:'" , "t-string: expecting '}', or format specs" ),
387+ ("t'{x!}'" , "t-string: missing conversion character" ),
388+ ("t'{x=!}'" , "t-string: missing conversion character" ),
389+ ("t'{x!z}'" , "t-string: invalid conversion character 'z': "
390+ "expected 's', 'r', or 'a'" ),
391+ ("t'{lambda:1}'" , "t-string: lambda expressions are not allowed "
392+ "without parentheses" ),
393+ ("t'{x:{;}}'" , "t-string: expecting a valid expression after '{'" ),
394+ ):
395+ with self .subTest (case ), self .assertRaisesRegex (SyntaxError , err ):
396+ eval (case )
397+
398+ def test_runtime_errors (self ):
381399 # Test missing variables
382400 with self .assertRaises (NameError ):
383401 eval ("t'Hello, {name}'" )
384402
385- # Test invalid conversion
386- num = 1
387- with self .assertRaises (SyntaxError ):
388- eval ("t'{num!z}'" )
389-
390403 def test_literal_concatenation (self ):
391404 # Test concatenation of t-string literals
392405 t = t "Hello, " t "world"
0 commit comments