Skip to content
This repository was archived by the owner on Nov 19, 2021. It is now read-only.

Commit 96253c3

Browse files
authored
Merge pull request #2 from zapier/single-quote-option-triples
Keep using double quotes for triple-quoted strings with --single-quote.
2 parents df83086 + 4cdef98 commit 96253c3

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ black [OPTIONS] [SRC]...
6969
Options:
7070
-l, --line-length INTEGER Where to wrap around. [default: 88]
7171
--single-quote Use single quotes instead of double quotes in
72-
strings.
72+
strings except for triple-quoted strings.
7373
--check Don't write the files back, just return the
7474
status. Return code 0 means nothing would
7575
change. Return code 1 means some files would be

black.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,17 +2338,19 @@ def normalize_string_quotes(leaf: Leaf, single_quote: bool = False) -> None:
23382338

23392339
quote_char = '"'
23402340
alt_quote_char = "'"
2341+
triple_quote_chars = '"""'
2342+
alt_triple_quote_chars = "'''"
23412343

23422344
if single_quote:
23432345
quote_char = "'"
23442346
alt_quote_char = '"'
23452347

2346-
if value[:3] == (quote_char * 3):
2348+
if value[:3] == triple_quote_chars:
23472349
return
23482350

2349-
elif value[:3] == (alt_quote_char * 3):
2350-
orig_quote = alt_quote_char * 3
2351-
new_quote = quote_char * 3
2351+
elif value[:3] == alt_triple_quote_chars:
2352+
orig_quote = alt_triple_quote_chars
2353+
new_quote = triple_quote_chars
23522354
elif value[0] == quote_char:
23532355
orig_quote = quote_char
23542356
new_quote = alt_quote_char
@@ -2381,9 +2383,9 @@ def normalize_string_quotes(leaf: Leaf, single_quote: bool = False) -> None:
23812383
leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
23822384
new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
23832385
new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
2384-
if new_quote == (quote_char * 3) and new_body[-1] == quote_char:
2386+
if new_quote == triple_quote_chars and new_body[-1] == triple_quote_chars[0]:
23852387
# edge case:
2386-
new_body = new_body[:-1] + "\\" + quote_char
2388+
new_body = new_body[:-1] + "\\" + triple_quote_chars[0]
23872389
orig_escape_count = body.count("\\")
23882390
new_escape_count = new_body.count("\\")
23892391
if new_escape_count > orig_escape_count:

0 commit comments

Comments
 (0)