Skip to content

Self-documenting f-string in conversion specifier throws ValueError #121130

Closed

Description

@JelleZijlstra

Bug report

Bug description:

Since Python 3.12, the compiler throws a ValueError when compiling a string like f"{x:{y=}}":

$ ./python.exe 
Python 3.14.0a0 (heads/main:81a654a342, Jun 28 2024, 07:45:17) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f"{x:{y=}}"
ValueError: field 'value' is required for Constant

Note this is a compile-time error; you also see it if you call compile() or ast.parse(). I would not expect the compiler to ever throw ValueError.

On 3.11, this works as I'd expect:

$ python
Python 3.11.9 (main, May  7 2024, 09:02:19) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f"{x:{y=}}"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> import ast
>>> ast.dump(ast.parse('f"{x:{y=}}"'))
"Module(body=[Expr(value=JoinedStr(values=[FormattedValue(value=Name(id='x', ctx=Load()), conversion=-1, format_spec=JoinedStr(values=[Constant(value='y='), FormattedValue(value=Name(id='y', ctx=Load()), conversion=114)]))]))], type_ignores=[])"

I don't have a use case for this, but came across it while exploring f-string syntax.

cc @ericvsmith for f-strings and @pablogsal because this feels related to PEP 701

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Jun 28, 2024
  2. pablogsal commented on Jun 28, 2024

    @pablogsal
    Member

    Hummm, I think this is a bug in 3.11 or at least some undefined situation. For example, with x=34 and y=20 in 3.11 this prints:

    >>> f"{x:{y=}}"
    'yyyyyyyyyyyyyyyyyy34'
    
  3. pablogsal commented on Jun 28, 2024

    @pablogsal
    Member

    There is not even an = sign there and I guess nobody here would have predicted that result so unless I am missing anything we should not allow this (and raise a proper error). @ericvsmith what do you think?

  4. JelleZijlstra commented on Jun 28, 2024

    @JelleZijlstra
    MemberAuthor

    @pablogsal I think that's the expected result, note that the self-documenting string is passed to x.__format__:

    >>> (34).__format__("y=20")
    'yyyyyyyyyyyyyyyyyy34'
    

    I don't know when this would be practically useful but I think it's at least consistent behavior.

  5. pablogsal commented on Jun 28, 2024

    @pablogsal
    Member

    Oh, I see! It's certainly surprising but I can see how this happens now much better! Thanks

  6. pablogsal commented on Jun 28, 2024

    @pablogsal
    Member

    CC @lysnikolaou WDYT?

  7. pablogsal commented on Jun 28, 2024

    @pablogsal
    Member

    We can fix this in the lexer, but this violates some assumptions (that only the topmost level can have debug expressions) so the fix is quite involved unless I am missing something.

  8. pablogsal commented on Jun 28, 2024

    @pablogsal
    Member

    I will try to give this a go to see if there is some easy way to handle this

  9. added a commit that references this issue on Jun 29, 2024
  10. ericvsmith commented on Jul 1, 2024

    @ericvsmith
    Member

    It's definitely an odd use case, but I think it should work.

    When testing, I often use datetime, since it's format spec is (mostly) pass-through:

    >>> import datetime
    >>> y = 3
    >>> f'{datetime.datetime.now():{y=}}'
    'y=3'
  11. added 5 commits that reference this issue on Jul 16, 2024
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 fixes3.13only security fixes3.14bugs and security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions