Skip to content

Commit 1cc778f

Browse files
committed
First pass at removing implicit Template/str concat
1 parent 5d5e187 commit 1cc778f

File tree

4 files changed

+19
-49
lines changed

4 files changed

+19
-49
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -999,13 +999,6 @@ def test_tstring(self):
999999
self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
10001000
self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)
10011001

1002-
# Test AST for implicit concat of t-string with f-string
1003-
tree = ast.parse('t"Hello {name}" f"{name}"')
1004-
self.assertIsInstance(tree.body[0].value, ast.TemplateStr)
1005-
self.assertIsInstance(tree.body[0].value.values[0], ast.Constant)
1006-
self.assertIsInstance(tree.body[0].value.values[1], ast.Interpolation)
1007-
self.assertIsInstance(tree.body[0].value.values[2], ast.FormattedValue)
1008-
10091002

10101003
class CopyTests(unittest.TestCase):
10111004
"""Test copying and pickling AST nodes."""

Lib/test/test_tstring.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -239,45 +239,20 @@ def test_literal_concatenation(self):
239239
self.assertTStringEqual(t, ("Hello, ", ""), [(name, "name")])
240240
self.assertEqual(fstring(t), "Hello, Python")
241241

242-
# Test concatenation with string literal
243-
name = "Python"
244-
t = t"Hello, {name}" "and welcome!"
245-
self.assertTStringEqual(
246-
t, ("Hello, ", "and welcome!"), [(name, "name")]
247-
)
248-
self.assertEqual(fstring(t), "Hello, Pythonand welcome!")
249-
250-
# Test concatenation with Unicode literal
251-
name = "Python"
252-
t = t"Hello, {name}" u"and welcome!"
253-
self.assertTStringEqual(
254-
t, ("Hello, ", "and welcome!"), [(name, "name")]
255-
)
256-
self.assertEqual(fstring(t), "Hello, Pythonand welcome!")
257-
258-
# Test concatenation with f-string literal
259-
tab = '\t'
260-
t = t"Tab: {tab}. " f"f-tab: {tab}."
261-
self.assertTStringEqual(t, ("Tab: ", ". f-tab: \t."), [(tab, "tab")])
262-
self.assertEqual(fstring(t), "Tab: \t. f-tab: \t.")
263-
264-
# Test concatenation with raw string literal
265-
tab = '\t'
266-
t = t"Tab: {tab}. " r"Raw tab: \t."
267-
self.assertTStringEqual(
268-
t, ("Tab: ", r". Raw tab: \t."), [(tab, "tab")]
269-
)
270-
self.assertEqual(fstring(t), "Tab: \t. Raw tab: \\t.")
271-
272-
# Test concatenation with raw f-string literal
273-
tab = '\t'
274-
t = t"Tab: {tab}. " rf"f-tab: {tab}. Raw tab: \t."
275-
self.assertTStringEqual(
276-
t, ("Tab: ", ". f-tab: \t. Raw tab: \\t."), [(tab, "tab")]
277-
)
278-
self.assertEqual(fstring(t), "Tab: \t. f-tab: \t. Raw tab: \\t.")
279-
242+
# Test disallowed mix of t-string and string
280243
what = 't'
244+
expected_msg = 'cannot mix str and Template literals'
245+
for case in (
246+
"t'{what}-string literal' 'str literal'",
247+
"t'{what}-string literal' u'unicode literal'",
248+
"t'{what}-string literal' f'f-string literal'",
249+
"t'{what}-string literal' r'raw string literal'",
250+
"t'{what}-string literal' rf'raw f-string literal'",
251+
):
252+
with self.assertRaisesRegex(SyntaxError, expected_msg):
253+
eval(case)
254+
255+
# Test disallowed mix of t-string and bytes
281256
expected_msg = 'cannot mix bytes and nonbytes literals'
282257
for case in (
283258
"t'{what}-string literal' b'bytes literal'",

Lib/test/test_unparse.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,6 @@ def test_tstrings(self):
206206
self.check_ast_roundtrip("t'foo'")
207207
self.check_ast_roundtrip("t'foo {bar}'")
208208
self.check_ast_roundtrip("t'foo {bar!s:.2f}'")
209-
self.check_ast_roundtrip("t'foo {bar}' f'{bar}'")
210-
self.check_ast_roundtrip("f'{bar}' t'foo {bar}'")
211-
self.check_ast_roundtrip("t'foo {bar}' fr'\\hello {bar}'")
212-
self.check_ast_roundtrip("t'foo {bar}' u'bar'")
213209

214210
def test_strings(self):
215211
self.check_ast_roundtrip("u'foo'")

Parser/action_helpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,11 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
18871887
return NULL;
18881888
}
18891889

1890+
if ((unicode_string_found || f_string_found) && t_string_found) {
1891+
RAISE_SYNTAX_ERROR("cannot mix str and Template literals");
1892+
return NULL;
1893+
}
1894+
18901895
// If it's only bytes or only unicode string, do a simple concat
18911896
if (!f_string_found && !t_string_found) {
18921897
if (len == 1) {
@@ -1902,6 +1907,7 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
19021907
}
19031908
}
19041909

1910+
19051911
if (t_string_found) {
19061912
return _build_concatenated_template_str(p, strings, lineno,
19071913
col_offset, end_lineno, end_col_offset, arena);

0 commit comments

Comments
 (0)