Skip to content

Wrong offsets of f-string elements in AST #104608

Closed as not planned
Closed as not planned
@sizmailov

Description

@sizmailov

Bug report

The elements of f-string ast contain the offsets of the f-string, not their own.

import ast
#         10        20        30
#123456789012345678901234567890
source = """\
f"aaa{1}bbb{2}ccc{3}"
"""
#123456789012345678901234567890
#         10        20        30

r = ast.parse(source)
for v in r.body[0].value.values:
    print(v.col_offset, v.end_col_offset, source[v.col_offset:v.end_col_offset])

Expected output:

2 5 aaa
6 7 1
8 10 bbb
12 13 2
14 17 ccc
18 19 3

Actual output:

0 21 f"aaa{1}bbb{2}ccc{3}"
0 21 f"aaa{1}bbb{2}ccc{3}"
0 21 f"aaa{1}bbb{2}ccc{3}"
0 21 f"aaa{1}bbb{2}ccc{3}"
0 21 f"aaa{1}bbb{2}ccc{3}"
0 21 f"aaa{1}bbb{2}ccc{3}"

Your environment

  • CPython versions tested on: Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
  • CPython versions tested on: Python 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0] on linux
  • Operating system and architecture: Ubuntu 22.04.1 LTS

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions