Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,8 @@ def check(test):
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]]')
Expand All @@ -1564,8 +1566,6 @@ def check(test):
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]]')
Expand All @@ -1586,6 +1586,7 @@ def check(test):
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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :exc:`SyntaxWarning` emitted for erroneous subscript expressions involving
:ref:`template string literals <t-strings>`. Patch by Brian Schubert.
12 changes: 7 additions & 5 deletions Python/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3617,10 +3618,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);
Expand Down Expand Up @@ -3674,6 +3676,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; "
Expand Down Expand Up @@ -3708,9 +3712,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; "
Expand Down
Loading