cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-121130: Fix f-string format specifiers with debug expressions

Open pablogsal opened this issue 1 year ago • 6 comments

  • Issue: gh-121130

pablogsal avatar Jun 29 '24 09:06 pablogsal

@lysnikolaou do you mind taking a look at this approach? I think is not too bad and it solidifies a bit more the concept of being in a format spec

pablogsal avatar Jun 30 '24 11:06 pablogsal

@lysnikolaou Ugh... this is much more complicated indeed. We need to restructure how we propagate the format specifier and how we capture the f-string expressions on format specifiers :(

pablogsal avatar Jul 01 '24 15:07 pablogsal

Also the AST is so weird because it removes {. Check this out:

python3.11 -m ast <<< 'f"{datetime.datetime.now():h1{y2=}}"'
Module(
   body=[
      Expr(
         value=JoinedStr(
            values=[
               FormattedValue(
                  value=Call(
                     func=Attribute(
                        value=Attribute(
                           value=Name(id='datetime', ctx=Load()),
                           attr='datetime',
                           ctx=Load()),
                        attr='now',
                        ctx=Load()),
                     args=[],
                     keywords=[]),
                  conversion=-1,
                  format_spec=JoinedStr(
                     values=[
                        Constant(value='h1y2='),
                        FormattedValue(
                           value=Name(id='y2', ctx=Load()),
                           conversion=114)]))]))],
   type_ignores=[])

notice that the node is h1y2 without the { in the middle :S

pablogsal avatar Jul 01 '24 15:07 pablogsal

I think this should do the trick. Boy this was hard to fix :S

pablogsal avatar Jul 01 '24 20:07 pablogsal

Hummm I will investigate the failure soon

pablogsal avatar Jul 01 '24 23:07 pablogsal

@lysnikolaou can you take another look so we can get this into rc1?

pablogsal avatar Jul 13 '24 13:07 pablogsal

I'm landing so this gets into the last 3.13 beta to get some coverage. We can fix anything @lysnikolaou doesn't like later

pablogsal avatar Jul 16 '24 18:07 pablogsal

Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. 🐍🍒⛏🤖

miss-islington-app[bot] avatar Jul 16 '24 18:07 miss-islington-app[bot]

Sorry, @pablogsal, I could not cleanly backport this to 3.13 due to a conflict. Please backport using cherry_picker on command line.

cherry_picker c46d64e0ef8e92a6b4ab4805d813d7e4d6663380 3.13

miss-islington-app[bot] avatar Jul 16 '24 18:07 miss-islington-app[bot]

Sorry, @pablogsal, I could not cleanly backport this to 3.12 due to a conflict. Please backport using cherry_picker on command line.

cherry_picker c46d64e0ef8e92a6b4ab4805d813d7e4d6663380 3.12

miss-islington-app[bot] avatar Jul 16 '24 18:07 miss-islington-app[bot]

GH-121868 is a backport of this pull request to the 3.13 branch.

bedevere-app[bot] avatar Jul 16 '24 18:07 bedevere-app[bot]

GH-122063 is a backport of this pull request to the 3.12 branch.

bedevere-app[bot] avatar Jul 20 '24 14:07 bedevere-app[bot]