]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-138372: Fix SyntaxWarning for erroneous t-string subscription (GH-138375...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 2 Sep 2025 17:39:08 +0000 (19:39 +0200)
committerGitHub <noreply@github.com>
Tue, 2 Sep 2025 17:39:08 +0000 (17:39 +0000)
gh-138372: Fix SyntaxWarning for erroneous t-string subscription (GH-138375)
(cherry picked from commit 5493b46462f93bfbca89599538887d598fca9d6f)

Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
Lib/test/test_grammar.py
Misc/NEWS.d/next/Core_and_Builtins/2025-09-02-09-10-06.gh-issue-138372.h1Xk4-.rst [new file with mode: 0644]
Python/codegen.c

index 7f5d48b9c63ab7d349df5ab68ca03eceab21a20c..cfb24a5c45782082ff0870ba301d25d0bc5c3d15 100644 (file)
@@ -1554,6 +1554,8 @@ class GrammarTests(unittest.TestCase):
         check('[None [i, j]]')
         check('[True [i, j]]')
         check('[... [i, j]]')
+        check('[t"{x}" [i, j]]')
+        check('[t"x={x}" [i, j]]')
 
         msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?'
         check('[(1, 2) [i, j]]')
@@ -1564,8 +1566,6 @@ class GrammarTests(unittest.TestCase):
         check('[f"x={x}" [i, j]]')
         check('["abc" [i, j]]')
         check('[b"abc" [i, j]]')
-        check('[t"{x}" [i, j]]')
-        check('[t"x={x}" [i, j]]')
 
         msg=r'indices must be integers or slices, not tuple;'
         check('[[1, 2] [3, 4]]')
@@ -1586,6 +1586,7 @@ class GrammarTests(unittest.TestCase):
         check('[[1, 2] [f"{x}"]]')
         check('[[1, 2] [f"x={x}"]]')
         check('[[1, 2] ["abc"]]')
+        msg=r'indices must be integers or slices, not string.templatelib.Template;'
         check('[[1, 2] [t"{x}"]]')
         check('[[1, 2] [t"x={x}"]]')
         msg=r'indices must be integers or slices, not'
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-02-09-10-06.gh-issue-138372.h1Xk4-.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-02-09-10-06.gh-issue-138372.h1Xk4-.rst
new file mode 100644 (file)
index 0000000..475e01c
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :exc:`SyntaxWarning` emitted for erroneous subscript expressions involving
+:ref:`template string literals <t-strings>`. Patch by Brian Schubert.
index 0488008cd7f16c989f2c4613cd2984ce5a54158f..7d62b41841ce814d6bb47cdf357221535e7fe7eb 100644 (file)
@@ -29,6 +29,7 @@
 #include "pycore_symtable.h"      // PySTEntryObject
 #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString
 #include "pycore_ceval.h"         // SPECIAL___ENTER__
+#include "pycore_template.h"      // _PyTemplate_Type
 
 #define NEED_OPCODE_METADATA
 #include "pycore_opcode_metadata.h" // _PyOpcode_opcode_metadata, _PyOpcode_num_popped/pushed
@@ -3607,10 +3608,11 @@ infer_type(expr_ty e)
         return &PyGen_Type;
     case Lambda_kind:
         return &PyFunction_Type;
-    case JoinedStr_kind:
     case TemplateStr_kind:
-    case FormattedValue_kind:
     case Interpolation_kind:
+        return &_PyTemplate_Type;
+    case JoinedStr_kind:
+    case FormattedValue_kind:
         return &PyUnicode_Type;
     case Constant_kind:
         return Py_TYPE(e->v.Constant.value);
@@ -3664,6 +3666,8 @@ check_subscripter(compiler *c, expr_ty e)
     case Set_kind:
     case SetComp_kind:
     case GeneratorExp_kind:
+    case TemplateStr_kind:
+    case Interpolation_kind:
     case Lambda_kind: {
         location loc = LOC(e);
         return _PyCompile_Warn(c, loc, "'%.200s' object is not subscriptable; "
@@ -3698,9 +3702,7 @@ check_index(compiler *c, expr_ty e, expr_ty s)
     case List_kind:
     case ListComp_kind:
     case JoinedStr_kind:
-    case TemplateStr_kind:
-    case FormattedValue_kind:
-    case Interpolation_kind: {
+    case FormattedValue_kind: {
         location loc = LOC(e);
         return _PyCompile_Warn(c, loc, "%.200s indices must be integers "
                                        "or slices, not %.200s; "