Add --single-quote option
hyperknot opened this issue ยท 17 comments
Prettier, the most successful "opinionated code formatter", probably an inspiration for this project, advertises exactly the same values as this project. In many ways they make even stricter formatting decisions than black.
Yet, even prettier supports a --single-quote
option.
Please add a --single-quote
option to black.
And before immediately closing this ticket, please understand there is a huge need from users to have this option. At least go as far as to search across Github for pyproject.toml
file's [tool.black]
and check how many major projects have to use black with skip-string-normalization = true
. Or simply listen to your users here.
I agree that having this option would make sense as the current situation is ignoring the community wishes. Currently it requires everyone who uses single quotes to run black and then have a second tool (we use pre-commit double-quote-string-fixer
) to ensure that single quotes are being used consistently.
I even think that removing skip-string-normalization
for single-quote
would be better as that is the most likely usage and skipping string normalization entirely is a strange option for a formatter.
Add this option is the outcome that would mostly satisfy #373 and #118
please understand there is a huge need from users to have this option.
What exactly is the need?
What exactly is the need?
To add an option to default to single quote instead of double quote.
Prettier's behaviour would be the best actually:
Double or single quotes? Prettier chooses the one which results in the fewest number of escapes.
"It's gettin' better!"
, not'It\'s gettin\' better!'
. In case of a tie, Prettier defaults to double quotes (but that can be changed via the --single-quote option).
@hyperknot black does exactly what you suggest in your last example. Black also chooses the one which results in the fewest number of escapes, and in the case of a tie it prefers double quotes.
Prettier defaults to double quotes (but that can be changed via the --single-quote option).
@zsol that's great to hear! I didn't know black has so high quality internal quote handling. Then adding this option is really just a matter of decision, and I kindly ask you to please consider our feature request.
The claim that single quotes are more Pythonic is dubious. PEP8 makes it clear neither is preferred:
In Python, single-quoted strings and double-quoted strings are the same. This PEP does not make a recommendation for this. Pick a rule and stick to it.
This is an opinionated formatter, it's important to choose one style and stick with it. I want to see black-formatted code and feel familiar with it. The fewer code-style options for my team to argue about, the better.
@bcb this is the difference between an individual and a community. You, as an individual are happy with the tool and that's fine. No one is proposing to take anything away from you or your team. Everything would work for you as it does today.
The community, on the other hand is not going to change their coding style based on the preference of a tool. Python is a mature language with lot of experienced, senior developers who have a strong opinion about the way they use quotation marks. If single quotation marks are not an option in black, they'll simply use skip-string-normalization
and hence taking away from the value of this tool.
The same happened in JS land. StandardJS was trying to force the JS ecosystem to semicolon less statement endings, and many couldn't accept that. Came prettier, which offered both options for semicolon and it became a community embraced project overnight!
One of the guiding principles of black is to offer only one style, so it's either going to prefer single or double quotes but not both. I would love to hear convincing arguments in favor of single quotes, which is why this issue is still open. Please focus the discussion on arguments for why single quotes should be the only thing black defaults to, instead of just a flavor tunable with configuration.
@zsol single and double quotes are about a 50-50 in the Python community. The whole community will never pick one over the other. Even the PEP guide says that both are ok. Then why would a tool like black think that it can change this?
If you want to support only one of them (doesn't matter which one), there'll be two consequences:
skip-string-normalization
usage on the short term- a fork appearing which offers this choice
Then instead of unifying Python developers, you've actually split the black community between two forks.
This is an opinionated formatter
Opinionated except for line-length, skip-string-normalization and skip-numeric-underscore-normalization
- line-length should have been set the PEP8 79 chars.
- skip-string-normalization actually create more options to argue about, so replacing it with single-quotes option feels a better approach for black.
- skip-numeric-underscore-normalization is being argued for removal in #549
@zsol All the arguments have been made already in #118 and #373 but I shall copy some over:
- Large projects have invested heavily in setting single quotes as the style guide and converting nearly every line of code is not going to be welcome just because black enforces double-quotes.
- Quote manipulation could be considered outside of a formatter and moving into the realm of refactoring.
- Many keyboard layout require using shift for double-quotes which is tedious and annoying when single-quote is unshifted. Since this is language dependant have an option is sensible.
- Python official documentation uses single quotes in code examples
- Python repr outputs single quotes
Please focus the discussion on arguments for why single quotes should be the only thing black does, instead of just a flavor tunable with configuration.
@zsol It's too late for that now, months ago perhaps it was possible but the traction that black has gained will cause a furore if you switched to enforcing single-quotes.
There are accessibility concerns regarding this choice as well that were overlooked in #118:
- As someone with a severe neuromuscular disorder (SMA) I require an on-screen keyboard to do anything (can only use a mouse). Each click is already an extremely difficult task and a double quote is 3 instead of 1: shift -> " -> unshift. I know that sounds mighty feeble, but it's true.
- As others have noted about double quotes being harsher on the eyes, this effect is exacerbated by AD(H)D. I've heard from multiple people with the condition that reading code (90% of what we all do) is markedly easier when single quotes are used throughout due to less visual noise.
Having a flag for this would be fantastic!
I didn't think of the accessibility concerns as well. But it might be one of the reasons why more and more code I read is standardising on single quotes, across languages which support both.
I added support for black -s|--single-quotes
. The accessibility concerns for on-screen keyboards, the visual complexity of DQ over SQ, and the RSI concerns from overuse of the left pinky compelled me to do it. Please kindly merge.
We will not be adding single quotes formatting to Black as previous discussions about it documented.