DEV Community

loading...

Enforcing single quotes in Python

davidarmendariz profile image David Armendáriz ・1 min read

So, I have tried black, yapf and autopep8 because those are the options that VS Code prompts me to use when I want to format on save.

Out of the 3, I find black the most useful, because it is more similar to Prettier. However, I don't like that it enforces me to use double quotes instead of single quotes. I have read in lots of places that this is not possible. I really like black but I find this limitation a HUGE disadvantage.

Do you know a way to enforce single quotes with black or do you prefer yapf or autopep8? Which one is more "flexible"? Do you use any of these formatters along with pylint?

Thanks for your answers!

Discussion (1)

pic
Editor guide
Collapse
rhymes profile image
rhymes

I use black and I'm fine with the double quotes. From their documentation:

Why settle on double quotes? They anticipate apostrophes in English text. They match the docstring standard described in PEP 257. An empty string in double quotes ("") is impossible to confuse with a one double-quote regardless of fonts and syntax highlighting used. On top of this, double quotes for strings are consistent with C which Python interacts a lot with.

On certain keyboard layouts like US English, typing single quotes is a bit easier than double quotes. The latter requires use of the Shift key. My recommendation here is to keep using whatever is faster to type and let Black handle the transformation.

from github.com/psf/black/blob/master/d...

they explain there's a way not to enforce quotes normalization using --skip-string-normalization but I've never tried it.

You can also use it with flake8 if you want to go further in syntax checking: black.readthedocs.io/en/stable/com...

My recommendation is to surrender to the double quotes :D