Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty string constant in f-string format_spec #110309

Closed
15r10nk opened this issue Oct 3, 2023 · 3 comments · Fixed by #110320
Closed

empty string constant in f-string format_spec #110309

15r10nk opened this issue Oct 3, 2023 · 3 comments · Fixed by #110320
Assignees
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes topic-parser type-bug An unexpected behavior, bug, or error

Comments

@15r10nk
Copy link
Contributor

15r10nk commented Oct 3, 2023

Bug report

Bug description:

The f-string f'{1:{name}}' has a extra Constant(value='') in the format_spec part in 3.12, which is not part of the AST in 3.11. This has also an effect on the generated bytecode.

import ast
import dis

print(ast.dump(ast.parse("f'{1:{name}}'"),indent=2))

dis.dis(compile("f'{1:{name}}'","<string>","exec"))

output (Python 3.12.0):

Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          FormattedValue(
            value=Constant(value=1),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                FormattedValue(
                  value=Name(id='name', ctx=Load()),
                  conversion=-1),
                Constant(value='')]))]))],            # <-- extra Constant
  type_ignores=[])
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 LOAD_NAME                0 (name)
              6 FORMAT_VALUE             0
              8 LOAD_CONST               1 ('')        # <-- extra LOAD_CONST
             10 BUILD_STRING             2             # <-- extra BUILD_STRING
             12 FORMAT_VALUE             4 (with format)
             14 POP_TOP
             16 RETURN_CONST             2 (None)

output (Python 3.11.3):

Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[
          FormattedValue(
            value=Constant(value=1),
            conversion=-1,
            format_spec=JoinedStr(
              values=[
                FormattedValue(
                  value=Name(id='name', ctx=Load()),
                  conversion=-1)]))]))],
  type_ignores=[])
  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 LOAD_NAME                0 (name)
              6 FORMAT_VALUE             0
              8 FORMAT_VALUE             4 (with format)
             10 POP_TOP
             12 LOAD_CONST               1 (None)
             14 RETURN_VALUE

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

@15r10nk 15r10nk added the type-bug An unexpected behavior, bug, or error label Oct 3, 2023
@AlexWaygood AlexWaygood added 3.12 bugs and security fixes 3.13 new features, bugs and security fixes labels Oct 3, 2023
@sunmy2019
Copy link
Member

Confirmed also on main: 8d92b6e

@pablogsal
Copy link
Member

@lysnikolaou @isidentical can you take a look? If you are not available I can take a look at this next week

@sunmy2019
Copy link
Member

I proposed a fix: apply the same empty constant prune algorithm here.

pablogsal pushed a commit that referenced this issue Oct 5, 2023
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes topic-parser type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants