-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Normalize string quotes #75
Conversation
FWIW, I am strongly opposed to this. It will take code that currently makes consistent use of quotes (eg a list of human-facing UI text strings that are all double quoted because they are likely to contain single-quote “apostrophes”, even though only some of them actually do), and make it needlessly inconsistent (the strings that happen to contain single quotes will remain double-quoted, the other ones will be converted to single quotes.) In addition to making the code uglier, this hurts maintainability; now I have to manually switch back to double quotes if I rewrite one of the strings in a way that adds a single quote. I am not at all convinced that changing quoting of strings should even be considered in scope for Black at all. If we really think it is, let’s please think through a more thorough solution before pushing something actively harmful. |
IMO you should just add a This discussion is relevant here: prettier/prettier#973 |
I commented about the master plan on the issue. Here I will just say, which is relevant to the pull request, that Carl absolutely convinced me not to ship a partial solution in this case. So, Zsolt, congratulations, you inherited a feature that is a bit bigger than just the first stab 😄 I also think if Black starts doing all three things in one update, it will cause less push back:
2 is already done. Now we have to look at 1, using Zsolt's specification table:
^1 Note: if a quote touches a closing triple quote of the same kind, it is tokenized as implicit string concatenation. In other words, ^2 Most f-strings won't embed any quotes within FormattedValues (e.g. the expressions within braces). So unless a quote of the same type appears inside an embedded expression, it's safe to do what we're doing with regular strings. ^3 I am responsible for the crappy fake parsing of f-strings in lib2to3 (e.g. we don't actually parse the embedded expressions). I'm sorry. But it was either that or no f-string support at all. Fortunately, for our purposes, we will only need to find embedded expressions (by matching pairs of non-escaped braces) to see if there are any quotes there. The grammar there is a bit tricky but there's relatively few rules: >>> f'{2 + 2}'
'4'
>>> f'{{double brace is an escape}}'
'{double brace is an escape}'
>>> f'{ {1:"embedded", 3: "dict"} }'
"{1: 'embedded', 3: 'dict'}"
>>> f'\{2 + 2}' # backslashes DON'T escape braces in f-strings
<stdin>:1: DeprecationWarning: invalid escape sequence \{
'\\4'
>>> f'\{2 + 2\}'
<stdin>:1: DeprecationWarning: invalid escape sequence \{
File "<stdin>", line 1
SyntaxError: f-string expression part cannot include a backslash |
eeb3505
to
4b8823e
Compare
Well I'm glad I'm not going to make things worse :) I expect to look at this again next weekend, stay tuned |
In terms of the test case I mentioned above, I don't think the full proposal outlined here improves anything. I don't want Black to ever turn this well-formatted and consistent code:
into this inconsistent mess:
because presence or absence of an apostrophe in a given message is an accidental and arbitrary, not inherent, characteristic of the string, and it's highly likely that some string in this list may be changed to include an apostrophe in the future. This is a concrete harm to both consistency and maintainability; the opposite of Black's goal. If a string changes from not having an apostrophe to having one, I either have to change its quoting manually or remember to type an escape, or else I've created a parsing error. And I have to first notice how the string is currently quoted, because Black has made them inconsistent where they were previously consistent. All in all, Black has directly made maintenance of this code noticeably worse than it was before; it's not a matter of aesthetic preference (on an aesthetic level I don't have any preference for one type of quote over another). This issue would also be largely resolved if Black standardizes on double quotes instead of single. IMO the prevalence of apostrophes alone far outweighs every argument for preferring single quotes; embedded double quotes are orders of magnitude rarer in practice. The extra keystroke on US keyboards is a non-issue; if you find it easier to type a single quote, just type single quotes and let Black convert them. |
Ha, Carl used my own argument against me here... and so I agree with him. Let's default to double quotes always. |
I thought I would mention that we need to consider PEP 257 (Docstring Convention) as well, specifically this:
|
Convert simple double-quoted strings to single-quoted. Convert triple (single) quoted strings to triple (double) quoted. Do not touch any strings that have backslashes or quotes inside the string. Fixes psf#51.
Pull Request Test Coverage Report for Build 138
💛 - Coveralls |
1 similar comment
Pull Request Test Coverage Report for Build 138
💛 - Coveralls |
I pushed a new implementation that tries to minimize escaping while preferring
I tried adding important edge cases to the test case, let me know if I missed anything! |
This is great. Thanks! ✨ 🍰 ✨ We'll iterate on it if anything comes up but looks good. Now for some documentation and README updates. |
Don't use single quote inside human readable strings, use |
Convert simple double-quoted strings to single-quoted. Convert triple (single) quoted strings to triple (double) quoted. Do not touch any strings that have backslashes or quotes inside the string.
Fixes #51.