Skip to content

feat:add f-string literal #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed

Conversation

Sarfia-786
Copy link
Contributor

@Sarfia-786 Sarfia-786 commented Apr 14, 2025

Pull Request description

This PR adds a new LiteralFormattedString class for representing formatted string values.
Resolves: #202

How to test these changes

  • ...

Pull Request checklists

Note:

This PR is a:

  • bug-fix
  • new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more
    complexity.
  • New and old tests passed locally.

Additional information

Reviewer's checklist

Copy and paste this template for your review's note:

## Reviewer's Checklist

- [ ] I managed to reproduce the problem locally from the `main` branch
- [ ] I managed to test the new changes locally
- [ ] I confirm that the issues mentioned were fixed/resolved .

Copy link
Contributor

@xmnlab xmnlab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @Sarfia-786 ! thanks for working on that.

I think it would be necessary to apply some changes here.

The idea is to mimic the python fstring, and here you can see some examples:

>>> import ast
>>> ast.dump(ast.parse("f'test{asdf}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=Name(id='asdf', ctx=Load()), conversion=-1)]))], type_ignores=[])"
>>> ast.dump(ast.parse("f'test{asdf + 1}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=BinOp(left=Name(id='asdf', ctx=Load()), op=Add(), right=Constant(value=1)), conversion=-1)]))], type_ignores=[])"
>>> ast.dump(ast.parse("f'test{asdf + 1}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=BinOp(left=Name(id='asdf', ctx=Load()), op=Add(), right=Constant(value=1)), conversion=-1)]))], type_ignores=[])"
>>> ast.dump(ast.parse("f'test{x:.2f}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=Name(id='x', ctx=Load()), conversion=-1, format_spec=JoinedStr(values=[Constant(value='.2f')]))]))], type_ignores=[])"
>>> 

so in other words, probably we will need to have a new class called FormattedValue with most of the structures you already implemented for
LiteralFormattedString and maybe the LiteralFormattedString would be a class to combine all the FormattedValue and LiteralString (non formatted) maybe similar to "JoinedStr".

it doesn't need to be the same as the python ast ... but 1) we should use the previous example as use cases and astx should support them, and it should be easy to have a 1:1 relation from astx and python ast.

does it make sense? ping me on discord if you have any questions

@Sarfia-786
Copy link
Contributor Author

hi @Sarfia-786 ! thanks for working on that.

I think it would be necessary to apply some changes here.

The idea is to mimic the python fstring, and here you can see some examples:

>>> import ast
>>> ast.dump(ast.parse("f'test{asdf}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=Name(id='asdf', ctx=Load()), conversion=-1)]))], type_ignores=[])"
>>> ast.dump(ast.parse("f'test{asdf + 1}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=BinOp(left=Name(id='asdf', ctx=Load()), op=Add(), right=Constant(value=1)), conversion=-1)]))], type_ignores=[])"
>>> ast.dump(ast.parse("f'test{asdf + 1}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=BinOp(left=Name(id='asdf', ctx=Load()), op=Add(), right=Constant(value=1)), conversion=-1)]))], type_ignores=[])"
>>> ast.dump(ast.parse("f'test{x:.2f}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='test'), FormattedValue(value=Name(id='x', ctx=Load()), conversion=-1, format_spec=JoinedStr(values=[Constant(value='.2f')]))]))], type_ignores=[])"
>>> 

so in other words, probably we will need to have a new class called FormattedValue with most of the structures you already implemented for LiteralFormattedString and maybe the LiteralFormattedString would be a class to combine all the FormattedValue and LiteralString (non formatted) maybe similar to "JoinedStr".

it doesn't need to be the same as the python ast ... but 1) we should use the previous example as use cases and astx should support them, and it should be easy to have a 1:1 relation from astx and python ast.

does it make sense? ping me on discord if you have any questions

@xmnlab done here #270, We can close it. I was stuck on some dependency issues, which is why I opened another PR.

@Sarfia-786 Sarfia-786 requested a review from xmnlab April 19, 2025 21:07
@Sarfia-786 Sarfia-786 closed this Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

**FormattedValue**: Represents f-string formatted values (e.g., {x:.2f}). Needed for modern string formatting.
2 participants