Skip to content

Commit 192fa90

Browse files
miss-islingtonbrianschubert
authored andcommitted
[3.14] pythongh-138372: Fix SyntaxWarning for erroneous t-string subscription (pythonGH-138375) (python#138392)
pythongh-138372: Fix SyntaxWarning for erroneous t-string subscription (pythonGH-138375) (cherry picked from commit 5493b46) Co-authored-by: Brian Schubert <[email protected]>
1 parent f15d5eb commit 192fa90

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

Lib/test/test_grammar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,8 @@ def check(test):
15541554
check('[None [i, j]]')
15551555
check('[True [i, j]]')
15561556
check('[... [i, j]]')
1557+
check('[t"{x}" [i, j]]')
1558+
check('[t"x={x}" [i, j]]')
15571559

15581560
msg=r'indices must be integers or slices, not tuple; perhaps you missed a comma\?'
15591561
check('[(1, 2) [i, j]]')
@@ -1564,8 +1566,6 @@ def check(test):
15641566
check('[f"x={x}" [i, j]]')
15651567
check('["abc" [i, j]]')
15661568
check('[b"abc" [i, j]]')
1567-
check('[t"{x}" [i, j]]')
1568-
check('[t"x={x}" [i, j]]')
15691569

15701570
msg=r'indices must be integers or slices, not tuple;'
15711571
check('[[1, 2] [3, 4]]')
@@ -1586,6 +1586,7 @@ def check(test):
15861586
check('[[1, 2] [f"{x}"]]')
15871587
check('[[1, 2] [f"x={x}"]]')
15881588
check('[[1, 2] ["abc"]]')
1589+
msg=r'indices must be integers or slices, not string.templatelib.Template;'
15891590
check('[[1, 2] [t"{x}"]]')
15901591
check('[[1, 2] [t"x={x}"]]')
15911592
msg=r'indices must be integers or slices, not'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`SyntaxWarning` emitted for erroneous subscript expressions involving
2+
:ref:`template string literals <t-strings>`. Patch by Brian Schubert.

Python/codegen.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "pycore_symtable.h" // PySTEntryObject
3030
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString
3131
#include "pycore_ceval.h" // SPECIAL___ENTER__
32+
#include "pycore_template.h" // _PyTemplate_Type
3233

3334
#define NEED_OPCODE_METADATA
3435
#include "pycore_opcode_metadata.h" // _PyOpcode_opcode_metadata, _PyOpcode_num_popped/pushed
@@ -3607,10 +3608,11 @@ infer_type(expr_ty e)
36073608
return &PyGen_Type;
36083609
case Lambda_kind:
36093610
return &PyFunction_Type;
3610-
case JoinedStr_kind:
36113611
case TemplateStr_kind:
3612-
case FormattedValue_kind:
36133612
case Interpolation_kind:
3613+
return &_PyTemplate_Type;
3614+
case JoinedStr_kind:
3615+
case FormattedValue_kind:
36143616
return &PyUnicode_Type;
36153617
case Constant_kind:
36163618
return Py_TYPE(e->v.Constant.value);
@@ -3664,6 +3666,8 @@ check_subscripter(compiler *c, expr_ty e)
36643666
case Set_kind:
36653667
case SetComp_kind:
36663668
case GeneratorExp_kind:
3669+
case TemplateStr_kind:
3670+
case Interpolation_kind:
36673671
case Lambda_kind: {
36683672
location loc = LOC(e);
36693673
return _PyCompile_Warn(c, loc, "'%.200s' object is not subscriptable; "
@@ -3698,9 +3702,7 @@ check_index(compiler *c, expr_ty e, expr_ty s)
36983702
case List_kind:
36993703
case ListComp_kind:
37003704
case JoinedStr_kind:
3701-
case TemplateStr_kind:
3702-
case FormattedValue_kind:
3703-
case Interpolation_kind: {
3705+
case FormattedValue_kind: {
37043706
location loc = LOC(e);
37053707
return _PyCompile_Warn(c, loc, "%.200s indices must be integers "
37063708
"or slices, not %.200s; "

0 commit comments

Comments
 (0)