GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Parts of strings that look like comments being stripped out of nested strings with debug specifier · Issue #135148 · python/cpython

Parts of strings that look like comments being stripped out of nested strings with debug specifier #135148

New issue
Copy link
New issue
Copy link
Closed
Closed
Parts of strings that look like comments being stripped out of nested strings with debug specifier#135148
Copy link
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@tom-pytel
tom-pytel
opened on Jun 4, 2025
Issue body actions

Bug report

Bug description:

Is this desired behavior?

$ ./python -m ast
f'{""" # this is part of the string, it shouldn't be stripped out
"""=}'
Module(
   body=[
      Expr(
         value=JoinedStr(
            values=[
               Constant(value='""" \n"""='),
               FormattedValue(
                  value=Constant(value=" # this is part of the string, it shouldn't be stripped out\n"),
                  conversion=114)]))])

$ ./python -m ast
t'{""" # this is part of the string, it shouldn't be stripped out
"""=}'
Module(
   body=[
      Expr(
         value=TemplateStr(
            values=[
               Constant(value='""" \n"""='),
               Interpolation(
                  value=Constant(value=" # this is part of the string, it shouldn't be stripped out\n"),
                  str='""" \n"""',
                  conversion=114)]))])

Even if they are not actually multiline:

$ python -m ast
t'{" # nooo "=}'
Module(
   body=[
      Expr(
         value=TemplateStr(
            values=[
               Constant(value='" '),
               Interpolation(
                  value=Constant(value=' # nooo '),
                  str='"',
                  conversion=114)]))])

Seems to go back to 3.12

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

No response

Linked PRs

  • gh-135148: Correctly handle f/t strings with comments and debug expressions #135198
  • [3.14] gh-135148: Correctly handle f/t strings with comments and debug expressions (GH-135198) #136720
  • [3.13] gh-135148: Correctly handle f/t strings with comments and debug expressions (#135198) #136899

Activity

Next
  1. tom-pytel
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 4, 2025
  2. JelleZijlstra commented on Jun 5, 2025

    @JelleZijlstra
    JelleZijlstra
    on Jun 5, 2025
    Member
    More actions

    Yeah this is clearly wrong. cc @pablogsal @lysnikolaou

  3. JelleZijlstra
    added
    release-blocker
    on Jun 5, 2025
  4. ezio-melotti
    added this to Release and Deferred blockers 🚫on Jun 5, 2025
  5. github-project-automation
    moved this to Todo in Release and Deferred blockers 🚫on Jun 5, 2025
  6. JelleZijlstra
    changed the title [-]Parts of strings that look like comments being stripped out of multiline strings[/-] [+]Parts of strings that look like comments being stripped out of nested strings with debug specifier[/+] on Jun 5, 2025
  7. JelleZijlstra commented on Jun 5, 2025

    @JelleZijlstra
    JelleZijlstra
    on Jun 5, 2025
    Member
    More actions

    This is actually a regression in 3.12, likely due to PEP-701:

    $ uv run --python=3.12 python
    Python 3.12.2 (main, May  6 2024, 20:50:52) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f'{" # no "=}'
    '" \' # no \''
    >>> 
    $ uv run --python=3.11 python
    Python 3.11.11 (main, Feb 12 2025, 15:06:01) [Clang 19.1.6 ] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> f'{" # no "=}'
    '" # no "=\' # no \''
    

    Removing the release blocker label as it's old.

  8. JelleZijlstra
    added
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    3.15bugs and security fixesbugs and security fixes
    and removed
    release-blocker
    on Jun 5, 2025
  9. tom-pytel commented on Jun 5, 2025

    @tom-pytel
    tom-pytel
    on Jun 5, 2025
    ContributorAuthor
    More actions

    Not just debug specifier, note the .str field:

    $ ./python -m ast
    t'{" # no "}'
    Module(
       body=[
          Expr(
             value=TemplateStr(
                values=[
                   Interpolation(
                      value=Constant(value=' # no '),
                      str='"',
                      conversion=-1)]))])
    

    Could this be problematic?

    >>> t'{" ! important information ! "}'.interpolations
    (Interpolation(' ! important information ! ', '" ! important information ! "', None, ''),)
    
    >>> t'{" # important information # "}'.interpolations
    (Interpolation(' # important information # ', '"', None, ''),)
  10. lysnikolaou commented on Jun 5, 2025

    @lysnikolaou
    lysnikolaou
    on Jun 5, 2025
    Member
    More actions

    Oof, yeah, this was probably introduced in #112284. We'll need to fix it, but the fix is not exactly straight-forward. I'll take a deeper look tomorrow.

  11. tom-pytel commented on Jun 5, 2025

    @tom-pytel
    tom-pytel
    on Jun 5, 2025
    ContributorAuthor
    More actions

    the fix is not exactly straight-forward.

    Not sure if this would apply in the context of the parser but worked for me before I realized it wasn't matching up.

    1. In range of interest exclude all lines which have multiline strings which do not end on the line.

    2. For each remaining line store maximum end column of expressions that end on that line and restrict comment checks to past this column.

  12. 2 remaining items

  13. pablogsal
    added 3 commits that reference this issue on Jun 6, 2025

    pythongh-135148: Correctly handle f/t strings with comments and debug…

    bd49471

    pythongh-135148: Correctly handle f/t strings with comments and debug…

    5f8456e

    pythongh-135148: Correctly handle f/t strings with comments and debug…

    ef9d22f
  14. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jun 7, 2025
  15. pablogsal
    added a commit that references this issue on Jul 16, 2025

    gh-135148: Correctly handle f/t strings with comments and debug expre…

    ef66fb5
  16. miss-islington
    added a commit that references this issue on Jul 16, 2025

    pythongh-135148: Correctly handle f/t strings with comments and debug…

    c08f2ab
  17. pablogsal
    added a commit that references this issue on Jul 16, 2025

    [3.14] gh-135148: Correctly handle f/t strings with comments and debu…

    9bf3464
  18. lysnikolaou
    added a commit that references this issue on Jul 21, 2025

    [3.13] gh-135148: Correctly handle f/t strings with comments and debu…

    c1085a0
  19. lysnikolaou commented on Jul 21, 2025

    @lysnikolaou
    lysnikolaou
    on Jul 21, 2025
    Member
    More actions

    All merged now.

  20. lysnikolaou
    closed this as completedon Jul 21, 2025
  21. github-project-automation
    moved this from Todo to Done in Release and Deferred blockers 🚫on Jul 21, 2025
  22. sobolevn
    marked f-string debug expressions containing hash '#' are malformed #137182 as a duplicate of this issue on Jul 28, 2025
  23. taegyunkim
    added a commit that references this issue on Aug 4, 2025

    pythongh-135148: Correctly handle f/t strings with comments and debug…

    941dba0
  24. Agent-Hellboy
    added a commit that references this issue on Aug 19, 2025

    pythongh-135148: Correctly handle f/t strings with comments and debug…

    a2fad4c
  25. kumaraditya303
    added a commit that references this issue on Sep 9, 2025

    [3.14] pythongh-135148: Correctly handle f/t strings with comments an…

    f671b96
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.12only security fixesonly security fixes3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Release and Deferred blockers 🚫
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.