8000 t-strings: Add more syntax tests (#68) · python/cpython@e6da757 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6da757

Browse files
authored
t-strings: Add more syntax tests (#68)
1 parent 0ee90f9 commit e6da757

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

Lib/test/test_grammar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,8 @@ def check(test):
15061506
check('[None (3, 4)]')
15071507
check('[True (3, 4)]')
15081508
check('[... (3, 4)]')
1509+
check('[t"{x}" (3, 4)]')
1510+
check('[t"x={x}" (3, 4)]')
15091511

15101512
msg=r'is not subscriptable; perhaps you missed a comma\?'
15111513
check('[{1, 2} [i, j]]')
@@ -1528,6 +1530,8 @@ def check(test):
15281530
check('[f"x={x}" [i, j]]')
15291531
check('["abc" [i, j]]')
15301532
check('[b"abc" [i, j]]')
1533+
check('[t"{x}" [i, j]]')
1534+
check('[t"x={x}" [i, j]]')
15311535

15321536
msg=r'indices must be integers or slices, not tuple;'
15331537
check('[[1, 2] [3, 4]]')
@@ -1548,6 +1552,8 @@ def check(test):
15481552
check('[[1, 2] [f"{x}"]]')
15491553
check('[[1, 2] [f"x={x}"]]')
15501554
check('[[1, 2] ["abc"]]')
1555+
check('[[1, 2] [t"{x}"]]')
1556+
check('[[1, 2] [t"x={x}"]]')
15511557
msg=r'indices must be integers or slices, not'
15521558
check('[[1, 2] [b"abc"]]')
15531559
check('[[1, 2] [12.3]]')

Lib/test/test_syntax.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,14 @@
17351735
Traceback (most recent call last):
17361736
SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
17371737
1738+
>>> t'{x}' = 42
1739+
Traceback (most recent call last):
1740+
SyntaxError: cannot assign to t-string expression here. Maybe you meant '==' instead of '='?
1741+
1742+
>>> t'{x}-{y}' = 42
1743+
Traceback (most recent call last):
1744+
SyntaxError: cannot assign to t-string expression here. Maybe you meant '==' instead of '='?
1745+
17381746
>>> (x, y, z=3, d, e)
17391747
Traceback (most recent call last):
17401748
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Lib/test/test_tstring.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)
0