Skip to content

SyntaxWarning for an invalid index type calls a t-string a str #138372

Closed

Description

@dscorbett

Bug report

Bug description:

When a t-string is used as an index, the SyntaxWarning calls it a str. The warning reports the correct type for other types of invalid expression in that context. The TypeError also reports the correct type.

>>> [][t""]
<python-input-0>:1: SyntaxWarning: list indices must be integers or slices, not str; perhaps you missed a comma?
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    [][t""]
    ~~^^^^^
TypeError: list indices must be integers or slices, not string.templatelib.Template

CPython versions tested on:

3.14

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Sep 2, 2025
  2. brianschubert commented on Sep 2, 2025

    @brianschubert
    Contributor

    This patch would be a possible fix:

    diff --git a/Python/codegen.c b/Python/codegen.c
    index 53388592330..0b8fb42376c 100644
    --- a/Python/codegen.c
    +++ b/Python/codegen.c
    @@ -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"
     
     #define NEED_OPCODE_METADATA
     #include "pycore_opcode_metadata.h" // _PyOpcode_opcode_metadata, _PyOpcode_num_popped/pushed
    @@ -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);
    $ ./python -c '[][t""]'
    <string>:1: SyntaxWarning: list indices must be integers or slices, not string.templatelib.Template; perhaps you missed a comma?
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
        [][t""]
        ~~^^^^^
    TypeError: list indices must be integers or slices, not string.templatelib.Template

    But I wonder if there's a reason it wasn't done this way in the first place?

    cc @lysnikolaou

  3. brianschubert commented on Sep 2, 2025

    @brianschubert
    Contributor

    Another related case:

    >>> [t"x" [1, 2]]
    <python-input-5>:1: SyntaxWarning: str indices must be integers or slices, not tuple; perhaps you missed a comma?
    Traceback (most recent call last):
      File "<python-input-5>", line 1, in <module>
        [t"x" [1, 2]]
         ~~~~~^^^^^^
    TypeError: 'string.templatelib.Template' object is not subscriptable

    Ideally, the SyntaxWarning should say that Template objects aren't subscriptable, similar to what's done for e.g. sets:

    >>> [{1} [1, 2]]
    <python-input-6>:1: SyntaxWarning: 'set' object is not subscriptable; perhaps you missed a comma?
    Traceback (most recent call last):
      File "<python-input-6>", line 1, in <module>
        [{1} [1, 2]]
         ~~~~^^^^^^
    TypeError: 'set' object is not subscriptable
  4. added a commit that references this issue on Sep 2, 2025
  5. added a commit that references this issue on Sep 2, 2025
  6. pablogsal commented on Sep 2, 2025

    @pablogsal
    Member

    Thanks a lot @brianschubert!

  7. added a commit that references this issue on Sep 2, 2025
  8. brianschubert commented on Sep 2, 2025

    @brianschubert
    Contributor

    Backport has been merged. Thanks for the report @dscorbett!

  9. added a commit that references this issue on Sep 9, 2025
  10. added a commit that references this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions