Skip to content

Commit 5493b46

Browse files
gh-138372: Fix SyntaxWarning for erroneous t-string subscription (#138375)
1 parent b3e785c commit 5493b46

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
@@ -3617,10 +3618,11 @@ infer_type(expr_ty e)
36173618
return &PyGen_Type;
36183619
case Lambda_kind:
36193620
return &PyFunction_Type;
3620-
case JoinedStr_kind:
36213621
case TemplateStr_kind:
3622-
case FormattedValue_kind:
36233622
case Interpolation_kind:
3623+
return &_PyTemplate_Type;
3624+
case JoinedStr_kind:
3625+
case FormattedValue_kind:
36243626
return &PyUnicode_Type;
36253627
case Constant_kind:
36263628
return Py_TYPE(e->v.Constant.value);
@@ -3674,6 +3676,8 @@ check_subscripter(compiler *c, expr_ty e)
36743676
case Set_kind:
36753677
case SetComp_kind:
36763678
case GeneratorExp_kind:
3679+
case TemplateStr_kind:
3680+
case Interpolation_kind:
36773681
case Lambda_kind: {
36783682
location loc = LOC(e);
36793683
return _PyCompile_Warn(c, loc, "'%.200s' object is not subscriptable; "
@@ -3708,9 +3712,7 @@ check_index(compiler *c, expr_ty e, expr_ty s)
37083712
case List_kind:
37093713
case ListComp_kind:
37103714
case JoinedStr_kind:
3711-
case TemplateStr_kind:
3712-
case FormattedValue_kind:
3713-
case Interpolation_kind: {
3715+
case FormattedValue_kind: {
37143716
location loc = LOC(e);
37153717
return _PyCompile_Warn(c, loc, "%.200s indices must be integers "
37163718
"or slices, not %.200s; "

0 commit comments

Comments
 (0)