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 — Decide how to handle comments with debug expressions in f-strings · Issue #112243 · python/cpython

Decide how to handle comments with debug expressions in f-strings #112243

New issue
Copy link
New issue
Copy link
Closed
Closed
Decide how to handle comments with debug expressions in f-strings#112243
Copy link

Description

@pablogsal
pablogsal
opened on Nov 18, 2023
Issue body actions

After PEP 701, having comments mixed with debug expressions is possible but the interactions has not been discussed. For instance, consider this code:

a = 1
f"{a = # my comment
}"

this produces:

'a = # my comment\n1'

which is surprising. What we should do here? I think we should probably not include the comment but we should agree on the behavior before a PR can be made

Linked PRs

  • gh-112243: Don't include comments in f-string debug expressions #112284
  • [3.12] gh-112243: Don't include comments in f-string debug expressions (GH-112284) #112285
Reacted by chincherpa

Activity

  1. pablogsal commented on Nov 18, 2023

    @pablogsal
    pablogsal
    on Nov 18, 2023
    MemberAuthor
    More actions

    @isidentical @lysnikolaou

  2. ericvsmith commented on Nov 18, 2023

    @ericvsmith
    ericvsmith
    on Nov 18, 2023
    Member
    More actions

    I think we should remove the comment and keep the newline.

    Reacted by Eli and Oh seungmin
  3. terryjreedy commented on Nov 18, 2023

    @terryjreedy
    terryjreedy
    on Nov 18, 2023
    Member
    More actions

    I expected the example to produce "SyntaxError: unterminated f-string literal (detected at line 1)", just as it would without the '{'. But given that '{' escapes the newline, I also expect it to escape the octothorpe, just as it does now. So I consider the current behavior correct.

    Reacted by Ethan Furman and Igor Morozov
  4. pablogsal commented on Nov 18, 2023

    @pablogsal
    pablogsal
    on Nov 18, 2023
    MemberAuthor
    More actions

    @terryjreedy not sure I follow your reasoning, what do you mean when you say that "given that '{' escapes the newline"? There is nothing here being scaped no? Inside an f-string expression now any expression can be made as if it written between parentheses. The key of this issue is the debug expression here

  5. terryjreedy commented on Nov 19, 2023

    @terryjreedy
    terryjreedy
    on Nov 19, 2023
    Member
    More actions

    I was using a different analogy for my expectation. 'a = #com\n3' is a SyntaxError. '''a= #com\n3''' is not, and the comment is keep. Your comment suggests this instead: (a * #com by itself is a Syntaxerror; (a * #com\n3 is not, because the parentheses cause the comment and newline to be ignored.

    Even better, I think, are these: f'{a #com\n}' ignores the comment and its terminating newline and prints 1. The a = construct is unusual because 'a =' is quoted while 'a' is executed. What happens with text between '=' and '}'? f'{a=x}' raises SyntaxError: f-string: expecting '!', or ':', or '}'. These examples convince me that a comment after '=' should be recognized as such and ignored rather that quoted as additional text.

    As for newlines: whitespace before and after = is kept, whether or not #whatever is present, and newlines can be escaped with \. Leave this alone.

  6. magistau commented on Nov 19, 2023

    @magistau
    magistau
    on Nov 19, 2023
    More actions

    If the expression component of an f-string requires comments, you're probably doing something horribly wrong. That being said, keeping the comment in the resulting string is likely to be the least surprising behavior. Either that or forbidding comments inside f-strings altogether.

  7. lysnikolaou commented on Nov 20, 2023

    @lysnikolaou
    lysnikolaou
    on Nov 20, 2023
    Member
    More actions

    I also think that the comment should be ignored, while the newline should be kept.

  8. pablogsal
    added 5 commits that reference this issue on Nov 20, 2023

    pythongh-112243: Don't include comments in f-string debug expressions

    f204fbd

    fixup! pythongh-112243: Don't include comments in f-string debug expr…

    2637415

    gh-112243: Don't include comments in f-string debug expressions (#112284

    d59feb5

    [3.12] pythongh-112243: Don't include comments in f-string debug expr…

    de4f091

    [3.12] gh-112243: Don't include comments in f-string debug expressions (

    7e70e2e
  9. pablogsal
    closed this as completedon Nov 20, 2023
  10. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-112243: Don't include comments in f-string debug expressions (…

    cd41ab6
  11. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-112243: Don't include comments in f-string debug expressions (…

    25649f7
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.