From 9cd796f925200a00b4e61d84e12e0b1736ad5d80 Mon Sep 17 00:00:00 2001 From: felix-hilden Date: Thu, 3 Jun 2021 23:47:28 +0300 Subject: [PATCH] Change rule to "r" last --- docs/the_black_code_style/current_style.md | 8 ++++---- src/black/strings.py | 14 +++++++------- src/blib2to3/pgen2/tokenize.py | 2 +- tests/data/python2.py | 4 ++-- tests/data/string_prefixes.py | 4 ++-- tests/data/string_quotes.py | 4 ++-- tests/test_black.py | 6 +++--- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/docs/the_black_code_style/current_style.md b/docs/the_black_code_style/current_style.md index b33714d823b..3e7825aab8e 100644 --- a/docs/the_black_code_style/current_style.md +++ b/docs/the_black_code_style/current_style.md @@ -225,10 +225,10 @@ escapes than before. _Black_ also standardizes string prefixes. Prefix characters are made lowercase with the exception of [capital "R" prefixes](#rstrings-and-rstrings), and in the case of multiple -characters "r" is put first as in spoken language: "raw f-string". 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. +characters "r" is put last as the common prefix and as required in Python 2. 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. The main reason to standardize on a single form of quotes is aesthetics. Having one kind of quotes everywhere reduces reader distraction. It will also enable a future version of diff --git a/src/black/strings.py b/src/black/strings.py index 7888b11ba46..30d307d3fcb 100644 --- a/src/black/strings.py +++ b/src/black/strings.py @@ -139,7 +139,7 @@ def normalize_string_prefix(s: str, remove_u_prefix: bool = False) -> str: new_prefix = orig_prefix.replace("F", "f").replace("B", "b").replace("U", "u") # Python syntax guarantees max 2 prefixes and that one of them is "r" - if len(new_prefix) == 2 and "r" != new_prefix[0].lower(): + if len(new_prefix) == 2 and "r" == new_prefix[0].lower(): new_prefix = new_prefix[::-1] if remove_u_prefix: new_prefix = new_prefix.replace("u", "") @@ -170,9 +170,9 @@ def normalize_string_quotes(s: str) -> str: return s # There's an internal error prefix = s[:first_quote_pos] - unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}") - escaped_new_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){new_quote}") - escaped_orig_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){orig_quote}") + unescaped_new_quote = re.compile(fr"(([^\\]|^)(\\\\)*){new_quote}") + escaped_new_quote = re.compile(fr"([^\\]|^)\\((?:\\\\)*){new_quote}") + escaped_orig_quote = re.compile(fr"([^\\]|^)\\((?:\\\\)*){orig_quote}") body = s[first_quote_pos + len(orig_quote) : -len(orig_quote)] if "r" in prefix.casefold(): if unescaped_new_quote.search(body): @@ -184,13 +184,13 @@ def normalize_string_quotes(s: str) -> str: new_body = body else: # remove unnecessary escapes - new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body) + new_body = sub_twice(escaped_new_quote, fr"\1\2{new_quote}", body) if body != new_body: # Consider the string without unnecessary escapes as the original body = new_body s = f"{prefix}{orig_quote}{body}{orig_quote}" - new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body) - new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body) + new_body = sub_twice(escaped_orig_quote, fr"\1\2{orig_quote}", new_body) + new_body = sub_twice(unescaped_new_quote, fr"\1\\{new_quote}", new_body) if "f" in prefix.casefold(): matches = re.findall( r""" diff --git a/src/blib2to3/pgen2/tokenize.py b/src/blib2to3/pgen2/tokenize.py index 2ad1d0aaed4..bad79b2dc2c 100644 --- a/src/blib2to3/pgen2/tokenize.py +++ b/src/blib2to3/pgen2/tokenize.py @@ -286,7 +286,7 @@ def compat(self, token: Tuple[int, Text], iterable: Iterable[TokenInfo]) -> None cookie_re = re.compile(r"^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)", re.ASCII) -blank_re = re.compile(rb"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII) +blank_re = re.compile(br"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII) def _get_normal_name(orig_enc: str) -> str: diff --git a/tests/data/python2.py b/tests/data/python2.py index 324985dc810..be5402185d3 100644 --- a/tests/data/python2.py +++ b/tests/data/python2.py @@ -5,7 +5,7 @@ print >> sys.stderr , "Warning:" , print >> sys.stderr , "this is a blast from the past." print >> sys.stderr , "Look, a repr:", `sys` -print >> sys.stderr , U"Even these prefixes are normalized!" +print >> sys.stderr , Ur"Even these prefixes are normalized!" def function((_globals, _locals)): @@ -25,7 +25,7 @@ def function((_globals, _locals)): print >>sys.stderr, "Warning:", print >>sys.stderr, "this is a blast from the past." print >>sys.stderr, "Look, a repr:", ` sys ` -print >>sys.stderr, u"Even these prefixes are normalized!" +print >>sys.stderr, ur"Even these prefixes are normalized!" def function((_globals, _locals)): diff --git a/tests/data/string_prefixes.py b/tests/data/string_prefixes.py index 904325b20bd..7c726b74f2a 100644 --- a/tests/data/string_prefixes.py +++ b/tests/data/string_prefixes.py @@ -20,5 +20,5 @@ ("", "") (r"", R"") -(rf"", rf"", Rf"", Rf"", rf"", rf"", Rf"", Rf"") -(rb"", rb"", Rb"", Rb"", rb"", rb"", Rb"", Rb"") +(fr"", fr"", fR"", fR"", fr"", fr"", fR"", fR"") +(br"", br"", bR"", bR"", br"", br"", bR"", bR"") diff --git a/tests/data/string_quotes.py b/tests/data/string_quotes.py index 5a4bc5d0b11..1cf16d2575d 100644 --- a/tests/data/string_quotes.py +++ b/tests/data/string_quotes.py @@ -23,7 +23,7 @@ r'Date d\'expiration:(.*)' r'Tricky "quote' r'Not-so-tricky \"quote' -rf'{yay}' +fr'{yay}' '\n\ The \"quick\"\n\ brown fox\n\ @@ -78,7 +78,7 @@ r"Date d\'expiration:(.*)" r'Tricky "quote' r"Not-so-tricky \"quote" -rf"{yay}" +fr"{yay}" "\nThe \"quick\"\nbrown fox\njumps over\nthe 'lazy' dog.\n" re.compile(r'[\\"]') "x = ''; y = \"\"" diff --git a/tests/test_black.py b/tests/test_black.py index 455cb33e827..33965f1026c 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -315,7 +315,7 @@ def test_expression_diff(self) -> None: expected, _ = read_data("expression.diff") tmp_file = Path(black.dump_to_file(source)) diff_header = re.compile( - rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " + fr"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" ) try: @@ -428,7 +428,7 @@ def test_skip_magic_trailing_comma(self) -> None: expected, _ = read_data("expression_skip_magic_trailing_comma.diff") tmp_file = Path(black.dump_to_file(source)) diff_header = re.compile( - rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " + fr"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" ) try: @@ -2050,7 +2050,7 @@ def test_bpo_2142_workaround(self) -> None: expected, _ = read_data("missing_final_newline.diff") tmp_file = Path(black.dump_to_file(source, ensure_final_newline=False)) diff_header = re.compile( - rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " + fr"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d " r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d" ) try: