Skip to content

Include the opening brace's line in multiline f-string errors #157778

Description

@pablogsal

An unclosed f-string replacement field can report an error on a later line, making the opening brace hard to find:

var = "abc"
a = f""" {var} extern "C" { """
b = """ string """
  File "script.py", line 3
    b = """ string """
                   ^
SyntaxError: f-string: expecting '}'

The quotes after the second { start a string inside the replacement field. That string ends on line 3, and the final quotes start another string that is never closed.

The error should include the line number of the opening brace:

SyntaxError: f-string: expecting '}' to close '{' on line 2

The same problem affects t-strings. Valid strings inside replacement fields should continue to work.

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Sep 19, 2026
  2. vstinner commented on Sep 20, 2026

    @vstinner
    Member

    Oh thanks for proposing a better error message for that case that I reported. The to close '{' on line 2 addition is very useful.

  3. added 2 commits that reference this issue on Sep 24, 2026
  4. vstinner commented on Sep 24, 2026

    @vstinner
    Member

    I confirm that the change works as expected. Updated output on the reproducer:

      File "/home/vstinner/python/main/script.py", line 3
        b = """ string """
                       ^
    SyntaxError: f-string: expecting '}' to close '{' on line 2
    

    The error message now shows where '{' was opened. Cool!

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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions