Skip to content

Bad f-string, causing CI issues #3531

Closed

Description

@fanninpm

Feature

Toy Example (adapted from extra_tests/snippets/fstrings.py, lines 47-48)

Code

foo = "bar"
part_spec = ">+#10x"
print(f"{16:0{part_spec}}{foo}")

CPython

00000+0x10bar

RustPython

SyntaxError: Got error in f-string: Invalid expression: invalid syntax. Got unexpected token 'part_spec' at line 1 column 9
print(f"{16:0{part_spec}}{foo}")
        ^

Real-world Example (adapted from CPython 3.9's Lib/test/test_fstring.py, lines 581-584)

Code

import decimal
width = 10
precision = 4
value = decimal.Decimal('12.34567')
print(f'result: {value:{width}.{precision}}')

CPython

result:      12.35

RustPython

SyntaxError: Got error in f-string: Invalid expression: invalid syntax. Got unexpected token '{' at line 1 column 9
print(f'result: {value:{width}.{precision}}')
        ^

Python Documentation

https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-python-grammar-format_spec

@coolreader18 this has been plaguing the CI for the past day or so (because of the --install-pip step).

Activity

  1. fanninpm commented on Feb 1, 2022

    @fanninpm
    ContributorAuthor

    @coolreader18 Update: I had an insight related to something in ast/src/ast_gen.rs, and I'm working on a thing.

  2. gavindsouza commented on Feb 3, 2022

    @gavindsouza

    I just tried rustpython get-pip.py on latest HEAD (bd295e0) and it threw

     File "/Users/Shared/projects/RustPython/vm/pylib-crate/Lib/zipimport.py", line 191, in get_filename
        code, ispackage, modpath = _get_module_code(self, fullname)
      File "/Users/Shared/projects/RustPython/vm/pylib-crate/Lib/zipimport.py", line 713, in _get_module_code
        code = _compile_source(modpath, data)
      File "/Users/Shared/projects/RustPython/vm/pylib-crate/Lib/zipimport.py", line 647, in _compile_source
        return compile(source, pathname, 'exec', dont_inherit=True)
    SyntaxError: Got error in f-string: Invalid expression: invalid syntax. Got unexpected token ',' at line 408 column 27
            completed_str = f"{completed_ratio:,.{precision}f}"
                              ^

    Extracted example:

    On CPython 3.9.9:

    >>> completed_ratio = 1212.121212
    >>> precision = 2
    >>> f"{completed_ratio:,.{precision}f}"
    '1,212.12'

    On Rust Python 0.1.2:

    >>>>> completed_ratio = 1212.121212
    >>>>> precision = 2
    >>>>> f"{completed_ratio:,.{precision}f}"
    SyntaxError: Got error in f-string: Invalid expression: invalid syntax. Got unexpected token ',' at line 1 column 3
    f"{completed_ratio:,.{precision}f}"
      ^

    @fanninpm Does your PR fix this too?

  3. fanninpm commented on Feb 3, 2022

    @fanninpm
    ContributorAuthor

    @fanninpm Does your PR fix this too?

    Not completely.

  4. youknowone commented on Feb 3, 2022

    @youknowone
    Member

    Once this is fixed, #3535 need to be reverted

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions