Skip to content
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

way to convert string to f-string #3757

Closed
brettcannon opened this issue Aug 16, 2021 · 5 comments
Closed

way to convert string to f-string #3757

brettcannon opened this issue Aug 16, 2021 · 5 comments

Comments

@brettcannon
Copy link
Member

Discussed in microsoft/vscode-python#15549

Originally posted by zcutlip March 3, 2021
I waste so many keystrokes by starting a string, realizing I want to format an object in the middle of it, skipping back to the beginning to prepend f, then returning to my typing position.

It'd be awesome if there was a keyboard shortcut for this, or, even better, if the need for an f-string could be automatically detected (e.g., the presence of curly braces and an in-scope object is referenced).


rgov on 3 Mar

I like the idea of just typing a curly bracket inside a string to convert it to an f-string, as long as this is pushed to the undo stack and I can Cmd-Z quickly to undo it. I type literal curly brackets in my strings infrequently enough it wouldn't bug me much (no more than other kinds of quote/bracket auto-complete).

I think the best way to do this would be to trigger on } and apply a few rules:

  1. Scan backwards for a matching { in the same string. If none is found, do not convert to an f-string. This avoids a number of false positives like print("int main() { return ", code, "}").

  2. Scan forwards and backwards for another } in the same string. If one is found, this probably means the string is not meant to be an f-string, and it guarantees that conversion will be attempted at most once per string.

Plus two heuristics for avoiding conversion of strings that are formatted with str.format():

  1. Do not convert if the sequence {} appears, because this is an invalid f-string specified and may indicate the use of str.format().

  2. Try to detect if str.format() is being called on the string. This is imperfect because if the user is typing a new line of code, it may not have been written yet. For existing code the user is editing, one could just see if .format( comes directly after the closing quote -- by far the most common pattern.

    But note that, as long as you are targeting a version of Python with f-strings, the user probably won't be writing new code that uses str.format().

@byehack
Copy link

byehack commented Aug 31, 2021

@brettcannon
Copy link
Member Author

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 5 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

@brettcannon
Copy link
Member Author

Thank you to everyone who upvoted this issue! Since the community showed interest in this feature request we will leave this issue open as something to consider implementing at some point in the future.

We do encourage people to continue 👍 this issue as it helps us prioritize our work based on what the community seems to want the most.

@brettcannon
Copy link
Member Author

I'm going to move this to Pylance as this seems to be a potential quick fix.

@brettcannon brettcannon transferred this issue from microsoft/vscode-python Dec 14, 2022
@rchiodo
Copy link
Contributor

rchiodo commented Dec 14, 2022

We actually already have an item for this:
#2615

@rchiodo rchiodo closed this as completed Dec 14, 2022
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

No branches or pull requests

3 participants