Single quotes black

When a python project lives for a long time without string format rules, one day it turns out that 90% of the code uses single quotes, and 10% double quotes.

Addendum flake8 quotes with the appropriate rules, it stopped letting new code with double quotes go beyond the pull request, but it began to require manual format editing in existing code, which I would like to avoid.

The first thought was to use black, but the format he proposes assumes the use of double quotes only. There was a request in github black in 2018 Single quote option string format, the discussion was heated, but it ended only with the introduction of the option --skip-string-normalizationwhich allowed not to touch the format of strings in the code being checked.

The easiest thing to do in this situation was to just go with black, but the team and most of the code were against it. I had to look for another solution, which turned out to be a modification of the black source code.

I must say right away that it was not possible to implement all the rules, since in flake8-quotes you can set different rules for docstring and other multiline strings, and the black logic, as far as we managed to figure it out, does not distinguish between them.

However, it was possible to deal with double quotes in simple strings,

and here’s what it took:

  • black 22.1.0 sources were downloaded (this is the first non-beta version of black and our CI is currently using this version in code review mode).

  • installed required libraries

For Python 3.8:
click             8.1.3
colorama          0.4.4
mypy-extensions   0.4.3
pathspec          0.9.0
pip               21.3.1
platformdirs      2.5.2
tomli             2.0.1
typing_extensions 4.2.0
wheel             0.37.1

The main changes were concentrated in the method normalize_string_quotes module src/black/stringы.py:

Method entry condition

    if value[:3] == '"""':
        return s

    elif value[:3] == "'''":
        orig_quote = "'''"
        new_quote="""""
    elif value[0] == '"':
        orig_quote="""
        new_quote = "'"
    else:
        orig_quote = "'"
        new_quote="""

replaced by

if value[:3] == '"""':
    return s

elif value[:3] == "'''":
    return s  # оставляем как есть и строки с ''', и строки с """
elif value[0] == '"':
    orig_quote="""
    new_quote = "'"
else:
    orig_quote = "'"
    new_quote="""

and the condition at the exit of the method

    if new_escape_count == orig_escape_count and orig_quote == '"':
        return s  # Prefer double quotes

replaced by

    if new_escape_count == orig_escape_count and orig_quote == "'":
        return s  # Prefer single quotes

After a few minutes of running the modified black, the problem of double quotes in our project was solved (well, there was still a stage of manual correction """ in multiline strings '''but it didn’t take long either).

From pleasant. The code contains several lines of the form u'\xa0' – apparently copied from tips for python 2 – and fixed (deleted u at the beginning). The escaped strings themselves have also been fixed. "\"" in '"'

From the unexpected. Strings fr'\abc' changed to rf'\abc'. Such edits, if necessary, were easily avoided by commenting out the method call normalize_string_prefix.

As a result, I would like to say that while doing all this, I almost agreed with the ideology of black, although the arguments of the apologists for single quotes – they do not require pressing Shift to enter, the Python interpreter itself in the console uses them, the code looks more transparent and thus more pythonic – also deserve attention.

As for opensource projects, here, I believe, everything has long been decided and black with its double quotes has won.

Similar Posts

Leave a Reply