Skip to content

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

Closed

Description

@tom-pytel

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

Activity

  1. added
    type-bug
    on Jun 4, 2025
  2. JelleZijlstra commented on Jun 5, 2025

    @JelleZijlstra
    Member

    Yeah this is clearly wrong. cc @pablogsal @lysnikolaou

  3. 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
  4. JelleZijlstra commented on Jun 5, 2025

    @JelleZijlstra
    Member

    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.

  5. added
    3.12
    3.13
    3.14
    3.15
    and removed on Jun 5, 2025
  6. tom-pytel commented on Jun 5, 2025

    @tom-pytel
    ContributorAuthor

    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, ''),)
  7. lysnikolaou commented on Jun 5, 2025

    @lysnikolaou
    Member

    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.

  8. tom-pytel commented on Jun 5, 2025

    @tom-pytel
    ContributorAuthor

    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.

  9. 2 remaining items

  10. added 3 commits that reference this issue on Jun 6, 2025
  11. added
    stdlib
    on Jun 7, 2025
  12. added a commit that references this issue on Jul 16, 2025
  13. added a commit that references this issue on Jul 16, 2025
  14. added a commit that references this issue on Jul 16, 2025
  15. added a commit that references this issue on Jul 21, 2025
  16. lysnikolaou commented on Jul 21, 2025

    @lysnikolaou
    Member

    All merged now.

  17. added a commit that references this issue on Aug 4, 2025
  18. added a commit that references this issue on Aug 19, 2025
  19. 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.123.133.143.15stdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions