Yapf: Settings for forcing/prefering single/double quotes for strings

Created on 28 Apr 2017  Â·  21Comments  Â·  Source: google/yapf

Just a setting so quoting style can be unified across a codebase.

e.g.

for the three strings abc, a'c, a"c

prefer_single_quotes -> 'abc', "a'c", 'a"c'
prefer_double_quotes -> "abc", "a'c", 'a"c'

force_single_quotes -> 'abc', 'a\'c', 'a"c'
force_double_quotes -> "abc", "a'c", "a\"c"

Most helpful comment

Hello!

I would like to communicate that this is also relevant to my interests!

Please implement this!

All 21 comments

As a useful reference, I use this tool for exactly this: https://github.com/myint/unify

I'm always preferring single quote while writing JavaScript, and eslint works well with it.

YAPF doesn't seem to replace triple single quotes with triple double quotes, even though PEP 8 specifies triple double quotes: https://www.python.org/dev/peps/pep-0008/#string-quotes

Is this feature on the roadmap at all? This really seems like a huge source of inconsistency. Even google internally forces quote symbol rules.

Would be really useful. Any progress on this?

Hello!

I would like to communicate that this is also relevant to my interests!

Please implement this!

Thanks for opening this thread. I'm also looking forward to this being implemented.

Bump

This is one aspect in which black is superior to yapf. From the relevant portion of the docs:

_Black_ prefers double quotes (" and """) over single quotes (' and '''). It
will replace the latter with the former as long as it does not result in more backslash
escapes than before.
(emphasis added)

It goes further:

_Black_ also standardizes string prefixes, making them always lowercase. On top of that,
if your code is already Python 3.6+ only or it's using the unicode_literals future
import, _Black_ will remove u from the string prefix as it is meaningless in those
scenarios.

Yapf has a philosophy of not changing the source at all, except for whitespace.

You could always format your code with black, then re-format it with yapf. ;)
This would also change some backslashes to parenthesized expressions, I think.

PS: pylint looks for the most common type of quotes and then complains when the other is used (except for cases like "'" and '"' which would otherwise be '\'' or "\"").

You could always format your code with black, then re-format it with yapf. ;)

Well, to be fair that's not at all a bad idea.

Yapf has a philosophy of not changing the source at all

[YAPF] takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide. (from the README)

@kamahen How are these two statements in concord? Does the README need to be changed?

Anyway, I normally use black instead of yapf, but one certain project I'm looking at contributing to has a style guide that dictates the usage of "single quotes for strings, or a double quote if the the string contains a single quote." This is where YAPF can help — by providing a means to specify a certain quote style in a project and to have that style automatically be applied.

From my experiences, black is superior. But I really really love 2-space indentation and black for-the-life-of-me refuse to include that option.
Sad.

@fanninpm "How are these two statements in concord? Does the README need to be changed?"

No.
Yapf only changes whitespace. It doesn't change quotes; it doesn't reformat comments; it doesn't add/remove backslash; it doesn't insert/remove parentheses.

(I would love it if yapf had some options for doing these things; but I don't have time to do any of them, and Emacs will reformat comments for me.)

Black is fine as long as you don't want any options. I've used pretty-printers like that in the past (Plus), and there are worse things (the Plus pretty-printer was great because my input was on punched cards, so minor edits to the code were much easier if I didn't have to worry about reformatting). I think (but can't remember for sure) that someone wrote a reformatter for BNR Protel; I do remember that we all agreed on a standard format and henceforth there was no religious arguing over code layout standards.

Yapf has a philosophy of not changing the source at all

[YAPF] takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide. (from the README)

@kamahen How are these two statements in concord? Does the README need to be changed?

We don't allow changes to the token stream. So we don't add, remove, or modify the tokens. This is why we don't allow adding a \-newline or rearranging imports, etc.

Anyway, I normally use black instead of yapf, but one certain project I'm looking at contributing to has a style guide that dictates the usage of "single quotes for strings, or a double quote if the the string contains a single quote." This is where YAPF can help — by providing a means to specify a certain quote style in a project and to have that style automatically be applied.

I've considered adding a feature where people could run their own transforms over the code. Similar to how lib2to3 does it with "fixes". (In fact, it would be exactly like that, because that's what we'll use.) That will allow them to use these transforms, but the onus is on the programmer to ensure it's safe to perform the transformations.

lib2to3 is going away; don't know if any of the potential successors will have a transformation library. All I've seen so far is that there are a couple of potential successors for parsing.

See https://bugs.python.org/issue40360: lib2to3 is now deprecated and may not be able to fully parse Python 3.10+. CPython encourages using https://libcst.readthedocs.io/ or https://parso.readthedocs.io/

https://github.com/pyga/awpa
https://github.com/Instagram/LibCST

LibCST builds on parso. Comparing stars on GitHub:

https://github.com/PyCQA/redbaron : 457
LibCST: 423
parso: 296
awpa: 10

https://bugs.python.org/issue40360#msg367726 and https://bugs.python.org/issue40360#msg367730

See if 521d61770aa51cc362c679bdd08f46699d243713 helps you with the quotes issue.

When will this be merged into master and released?

Yeah, what's the state of this? This commit doesn't seem to be in master...

You could always format your code with black, then re-format it with yapf. ;)

Well, to be fair that's not at all a bad idea.

It is a bad idea: Editors can usually call black or yapf for formatting, but not both...

It is a bad idea: Editors can usually call black or yapf for formatting, but not both...

I beg to differ. I believe it's not impossible to set up a sequence of formatters with specific orders -- I know for a fact Emacs can do this in a breeze.

Yapf has a philosophy of not changing the source at all

This is a well-intentioned philosophy, but YAPF would be a far more useful tool if the philosophy was for this to merely be the default behaviour. I've seen hundreds of hours wasted by devs not being able to find code because of these things, and the resulting formatting arguments. The more that can be enforced by code, saving devs from having to worry about it, the better, IMHO.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jrwrigh picture jrwrigh  Â·  6Comments

MagSec-Arts picture MagSec-Arts  Â·  3Comments

stevenleeS0ht picture stevenleeS0ht  Â·  4Comments

hayd picture hayd  Â·  8Comments

samypr100 picture samypr100  Â·  6Comments