-
-
Notifications
You must be signed in to change notification settings - Fork 186
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
Use original source when building f-strings #298
Conversation
the current approach is intentional, to avoid putting complex expressions into fstrings |
Maybe, but it misses a lot of simple expressions too, such as |
your approach also breaks for what it's worth, there's helpers already in pyupgrade to extract the positional arguments of function calls -- you can use those to do more expressions, but please keep the args which are upgraded simple :) |
I handle Yeah that’s what I started out doing, but I kept finding that I was writing more and more complicated code just to get a better approximation of the identity function. So what do you think about trying to do this at the token level? |
(not sure if the comments raced, but I addressed this 😉)
|
Right, that’s what I was replying to to when I said “that’s what I started out doing”. But
|
And I don’t think any definition of “simple” is going to satisfy everyone. One can make “simple” expressions using any of a wide variety of syntax elements, or “complicated” expressions using a very limited set of syntax elements repeatedly. At least the string-based definition (does not contain quotes or backslash or newline) is objective, and a user can always extract a variable if they don’t like the result. But that’s just my view. |
(2) is very easily fixable, but also is forbidden in the fstrings rewriter now (and would only apply to type annotations where I believe such a thing isn't possible) I forget the name of the helper but you essentially get this information out of it at the token level which you can use to exactly preserve the source (I probably should have used it to do the typing classes, but decided it was easier to cut corners and go the unparse route since there's very few things that can be type annotations)
|
Oh I see. Yes, that sounds helpful. |
c1ccf9e
to
7ab3bf6
Compare
I rewrote this to use |
I was going to take a stab at implementing the below conversions but that would probably conflict with this PR. Seeing how this PR had no activity for months I don't expect it to get merged or closed any time soon. "{foo}".format(**locals()) # f"{foo}"
"{foo}".format(**globals()) # f"{foo}" |
ah yeah I probably should have merged this before rewriting the way pyupgrade is organized :( |
Rebased on v2.10.0. |
Rebased for a simple conflict with #417. Still interested in merging this? There’s no time like the present. 🙂 |
Signed-off-by: Anders Kaseorg <[email protected]>
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.
Working at the AST level meant we only handled a small subset of Python when generating f-strings. By extracting the original code as a string we can handle many more cases.
A disadvantage of this approach is that it requires Python ≥ 3.8 forThis now uses the token stream like everything else, avoiding that requirement.ast.expr.end_lineno
,ast.expr.end_col_offset
.