News for hackers. Stuff that matters.
Re:Black, the uncompromising Python code formatter, is stable by formerly_proven on Saturday January 29, 2022 @04:11PM (#30130507)
> Black prefers [i.e. converts] double quotes (" and """) over single quotes (' and ''').

Can someone explain this to me? Why would you ever prefer " over ' in a language where both can be used equally?

Re:Black, the uncompromising Python code formatter, is stable by drekipus on Saturday January 29, 2022 @04:16PM (#30130564)
It's a tic for me too, as I always prefer '.

But to be fair I think it's because 1. Double quotes are ""universally"" strings in other languages 2. Having apostrophes gets annoying.

The great benefit really is that I can write single word strings just fine, and black will adjust it for me. I get my cake, and so do the others.

Re:Black, the uncompromising Python code formatter, is stable by Teongot on Saturday January 29, 2022 @04:13PM (#30130525)
Because "don't" is easier to read than 'don\'t'
Re:Black, the uncompromising Python code formatter, is stable by JimDabell on Sunday January 30, 2022 @01:01AM (#30134512)
If you use proper apostrophes instead of ASCII apostrophe / single quote, you don’t have to escape anything and you get better typography. "don’t" is easier to read than both.

If you’re on a Mac, Option-Shift-] will type it.

Re:Black, the uncompromising Python code formatter, is stable by JackFr on Saturday January 29, 2022 @04:39PM (#30130796)
When you’re embedding SQL in a string, a you use ‘ a lot more than “. Not the only use case, but one to give some consideration.
Re:Black, the uncompromising Python code formatter, is stable by throwaway894345 on Saturday January 29, 2022 @05:10PM (#30131084)
I think that’s only MySQL. Postgres uses double quotes for identifiers and single quotes for literals.

EDIT: I misinterpreted that single quote as a backtick. In any case, both single and double quotes are common in SQL, but single quotes are a bit more common.

Re:Black, the uncompromising Python code formatter, is stable by shpx on Saturday January 29, 2022 @04:36PM (#30130763)
Black will use single quotes if your string contains double quotes and no single quotes. It'll reformat

     print("\"Hello\" he said")
to

     print('"Hello" he said')
I think pretty much all formatters for languages that let you use either quote do this. It makes sense.
Re:Black, the uncompromising Python code formatter, is stable by iqanq on Saturday January 29, 2022 @04:25PM (#30130644)
And 'do not' is easier to read than both :)
Re:Black, the uncompromising Python code formatter, is stable by TameAntelope on Saturday January 29, 2022 @04:25PM (#30130646)
Arguing over Black formatting choices is a little ironic, isn’t it?

The whole point is that they’re all arbitrary so just let the defaults win, and adjust yourself instead.

Re:Black, the uncompromising Python code formatter, is stable by WillDaSilva on Saturday January 29, 2022 @07:29PM (#30132201)
The presence and depth of the debate about this formatting choice suggests that it isn't arbitrary. Many have provided solid reasons for why one option should be preferred over the other. They aren't merely articulating their opinions; they are also providing objective reasons for why their opinion is valid.
Re:Black, the uncompromising Python code formatter, is stable by TameAntelope on Saturday January 29, 2022 @07:42PM (#30132319)
Everything about formatting is arbitrary, as long as it's consistent in a given codebase to the point of predictability (e.g. 90% consistent, not necessarily 100% consistent).

To elaborate a bit, sure, there are arguments for everything talked about here, and they're valid, but the marginal utility of any one choice is dwarfed by opting out of the arguments entirely, and just going with whatever Black has selected as a default.

Re:Black, the uncompromising Python code formatter, is stable by teddyh on Saturday January 29, 2022 @04:16PM (#30130562)
Because most commonly used languages use double quotes, not single quotes, for string literals. Some languages can use both, either interchangeably or with slightly different semantics, but others either uses single quotes for other purposes, or do not use them for anything at all. Therefore, double quotes for strings look more normal, so to speak, in source code.
Re:Black, the uncompromising Python code formatter, is stable by tsimionescu on Saturday January 29, 2022 @05:27PM (#30131211)
Except that in the language you're most likely to mix with Python, SQL, strings are in single quotes (double quotes are for identifiers like column names, especially if they're case sensitive). So it's probably a bad choice on Black's part.
Re:Black, the uncompromising Python code formatter, is stable by teddyh on Saturday January 29, 2022 @07:12PM (#30132080)
(MySQL, notoriously, uses double quotes for strings.)

However, does not Python and standard SQL using different quote characters make it easier? I.e.

  query = "SELECT column_a FROM a_table WHERE column_b = 'foo';"
compared to this, if single quotes were enforced:

  query = 'SELECT column_a FROM a_table WHERE column_b = \'foo\';'
Re:Black, the uncompromising Python code formatter, is stable by tsimionescu on Saturday January 29, 2022 @08:24PM (#30132703)
If it's useful to use the same string literal character "as everyone else", it would be useful to use the same as a language you are very likely to see combined - so, single quote.

On the other hand, if it's useful to have minimum need of escaping, then single quote is still better, because in SQL it's much more common to quote identifier names than to have literal strings.

  query = 'SELECT "columnA" FROM "aTable" WHERE "columnB" = \'foo\';'
compared to

  query = "SELECT \"columnA\" FROM \"aTable\" WHERE \"columnB\" = 'foo';"
Re:Black, the uncompromising Python code formatter, is stable by teddyh on Monday January 31, 2022 @02:49AM (#30145553)
> it would be useful to use the same as a language you are very likely to see combined - so, single quote.

Why? It’s obvious that it causes issues as I showed, and yet you simplay claim the opposite without argument? The difference between other programming languages and SQL is, of course, that it’s very common to have embedded SQL code in your program, but not common to have embedded strings of code in another programming language.

> in SQL it's much more common to quote identifier names than to have literal strings.

That is certainly not true. The kind of SQL you quote is only found in auto-generated SQL by ORMs and the like, and those are not likely to exist as strings in code. To use your example, this is the normal SQL way to write that:

  query = "SELECT columnA FROM aTable WHERE columnB = 'foo';"
I.e. no double quotes needed; no need to escape anything.
Re:Black, the uncompromising Python code formatter, is stable by xigoi on Saturday January 29, 2022 @07:22PM (#30132147)
Using double quotes makes it easier to have single quotes inside the string literals.
Re:Black, the uncompromising Python code formatter, is stable by ambivalence on Saturday January 29, 2022 @04:16PM (#30130566)
This was decided very early in the project, the discussion can be read here: https://github.com/psf/black/issues/51
Re:Black, the uncompromising Python code formatter, is stable by wging on Saturday January 29, 2022 @04:20PM (#30130597)
Re:Black, the uncompromising Python code formatter, is stable by formerly_proven on Saturday January 29, 2022 @05:28PM (#30131219)
Did you lock that issue before posting it here? Is this some kind of sore/forbidden topic for the project?

Edit: I suppose the question is superfluous seeing how many replies there are just here.

Re:Black, the uncompromising Python code formatter, is stable by ambivalence on Saturday January 29, 2022 @05:44PM (#30131360)
Yes, and yes. Edit: and yes.
Re:Black, the uncompromising Python code formatter, is stable by kstrauser on Saturday January 29, 2022 @04:52PM (#30130912)
That was the thing that bugged me at first, but other people on my team loved it. Some things bugged other people on my team, even though I love them. If a tool can manage to annoy everyone about the same amount, but in different areas, it’s doing something right.

And while it’s still kind of strange that `a = “foo”` looks different than `repr(a)`, after a while of using Black I don’t notice it anymore.

Re:Black, the uncompromising Python code formatter, is stable by breput on Saturday January 29, 2022 @04:15PM (#30130544)
A couple of possible reasons:

* Double quotes are more visible, especially for triple quotes.

* It makes it easier to have text without having to manually escape apostrophes.

Re:Black, the uncompromising Python code formatter, is stable by indymike on Saturday January 29, 2022 @04:21PM (#30130609)
This is simple: the default is picked, so now no one has to argue about it.
Re:Black, the uncompromising Python code formatter, is stable by HALtheWise on Saturday January 29, 2022 @05:27PM (#30131217)
Additionally, standardizing all quotes makes searching for a specific string literal across a codebase easier, since you don't need regex to match against both quote types. Once you've decided to standardize strings, it doesn't matter much which one you pick.

Personally, I still type single quotes in almost all cases, and just let Black reformat to double quotes to save shift key presses.

Re:Black, the uncompromising Python code formatter, is stable by odiroot on Saturday January 29, 2022 @04:13PM (#30130517)
I prefer them too. Makes it easy to notice than single ones. It's just a personal preference, there's no big logic behind it.
Re:Black, the uncompromising Python code formatter, is stable by hadcomplained on Saturday January 29, 2022 @09:48PM (#30133369)
The other way around (converting double quoted strings to single quoted ones) would be more understandable, as that is what str.__repr__ prefers:

    >>> "string"
    'string'
I sense a bloated ego of the author in Black's decisions -- to contradict with the language's preferred formatting style.
Re:Black, the uncompromising Python code formatter, is stable by nopenopenopeno on Saturday January 29, 2022 @04:20PM (#30130600)
It’s easier and faster to differentiate between “ and ` than between ‘ and `.
Re:Black, the uncompromising Python code formatter, is stable by floober on Saturday January 29, 2022 @04:12PM (#30130515)
Uniformity, presumably
Re:Black, the uncompromising Python code formatter, is stable by mixmastamyk on Saturday January 29, 2022 @05:01PM (#30130986)
Try nero from PyPI instead, I’ll upgrade to stable shortly.
Re:Black, the uncompromising Python code formatter, is stable by rirze on Saturday January 29, 2022 @05:12PM (#30131100)
Last commit was from 2019? It sounds like you're not automating the patch for every black release...
Re:Black, the uncompromising Python code formatter, is stable by mixmastamyk on Saturday January 29, 2022 @06:02PM (#30131497)
Things were factored a few times, wasn’t a consistent patch. Also I didn’t care for a few of the changes so didn’t upgrade yet. However stable should be a good target for automation.
Re:Black, the uncompromising Python code formatter, is stable by faangiq on Saturday January 29, 2022 @04:27PM (#30130670)
In some languages single vs double has different functionality.