Skip to content

Incorrect literals in FormattedValue #160

@JoshuaGlaZ

Description

@JoshuaGlaZ

FormattedValue (fstring) in noworkflow are incorrectly capturing entire string literals instead of parsing and capturing only the literal parts

f"Hello, my name is {name} and I am {age} years old."

will give values

name type
f"Hello, my name is {name} and I am {age} years old." fstring
f"Hello, my name is {name} and I am {age} years old." literal
{name} fvalue
f"Hello, my name is {name} and I am {age} years old." literal
{age} fvalue
f"Hello, my name is {name} and I am {age} years old." literal

but the correct literal should captured separately the formatted value (fvalues), as demonstrated in the example below using ast.dump:

Module(
  body=[
    Expr(
      value=JoinedStr(
        values=[      
          Constant(value='Hello, my name is '),
          FormattedValue(
            value=Name(id='name', ctx=Load()),
            conversion=-1),
          Constant(value=' and I am '),
          FormattedValue(
            value=Name(id='age', ctx=Load()),
            conversion=-1),
          Constant(value=' years old.')]))],
  type_ignores=[])

In noworkflow, the equivalent representation should be

name type
f"Hello, my name is {name} and I am {age} years old." fstring
"Hello, my name is " literal
{name} fvalue
" and I am " literal
{age} fvalue
" years old." literal

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions