-
Notifications
You must be signed in to change notification settings - Fork 25
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
Conversation
There was a problem hiding this 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
@xmnlab done here #270, We can close it. I was stuck on some dependency issues, which is why I opened another PR. |
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:
proposed in this PR, in both image and ASCII formats. For more
information, check this Google Colab notebook:
https://colab.research.google.com/drive/1xXwHmOMkJKFSmhRvn4WYfSAsdDzMnawf?usp=sharing
This PR is a:
About this PR:
Author's checklist:
complexity.
Additional information
Reviewer's checklist
Copy and paste this template for your review's note: