Skip to content

Column offsets in nested f-string expressions are wrong #114

Closed
python/cpython
#19771

Description

@lysnikolaou

Parsing the following:

expr = """
a = 10
f'{a * f"-{x()}-"}'"""

produces the following AST (not complete):

...
op=Mult(),
right=JoinedStr(
    values=[
        Constant(
            value="-",
            lineno=1,
            col_offset=5,
            end_lineno=1,
            end_col_offset=15,
        ),
        FormattedValue(
            value=Call(
                func=Name(
                    id="x",
                    ctx=Load(),
                    lineno=1,
                    col_offset=9,
                    end_lineno=1,
                    end_col_offset=10,
                ),
                args=[],
                keywords=[],
                lineno=1,
                col_offset=9,
                end_lineno=1,
                end_col_offset=12,
            ),
            conversion=-1,
            lineno=1,
            col_offset=5,
            end_lineno=1,
            end_col_offset=15,
        ),
        Constant(
            value="-",
            lineno=1,
            col_offset=5,
            end_lineno=1,
            end_col_offset=15,
        ),
                            ],
lineno=3,
col_offset=7,
end_lineno=3,
end_col_offset=17,
...

Activity

  1. lysnikolaou commented on Apr 21, 2020

    @lysnikolaou
    MemberAuthor

    @pablogsal @gvanrossum Is there a way to fix this in a way that doesn't require too much effort? I can't think of something.

  2. added
    3.9.0b1Needs to be fixed/implemented until the 3.9.0b1 release
    and removed
    help wantedExtra attention is needed
    on Apr 27, 2020
  3. changed the title [-]Line numbers in nested f-string expressions are wrong[/-] [+]Column offsets in nested f-string expressions are wrong[/+] on Apr 28, 2020
  4. lysnikolaou commented on Apr 28, 2020

    @lysnikolaou
    MemberAuthor

    Line numbers were fixed by pre-feeding starting line and column numbers to the parser in 991a634. There is still a bug with the column offsets, because the node shifting of the nested expression is executed before that of the outer one.

  5. pablogsal commented on Apr 28, 2020

    @pablogsal

    There is still a bug with the column offsets, because the node shifting of the nested expression is executed before that of the outer one.

    Would a single node-shift of the whole f-string tree work? Or we do require two (ordered) shifts?

  6. lysnikolaou commented on Apr 28, 2020

    @lysnikolaou
    MemberAuthor

    There is still a bug with the column offsets, because the node shifting of the nested expression is executed before that of the outer one.

    Would a single node-shift of the whole f-string tree work? Or we do require two (ordered) shifts?

    That might work, yeah! I'll work on it in a bit.

  7. added a commit that references this issue on Apr 28, 2020
    01c881f
  8. self-assigned this
    on Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.9.0b1Needs to be fixed/implemented until the 3.9.0b1 release

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions