]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Normalise string prefix order (#2297)
authorFelix Hildén <felix.hilden@gmail.com>
Thu, 13 Jan 2022 17:59:43 +0000 (19:59 +0200)
committerGitHub <noreply@github.com>
Thu, 13 Jan 2022 17:59:43 +0000 (09:59 -0800)
Closes #2171

CHANGES.md
docs/the_black_code_style/current_style.md
src/black/strings.py
src/blib2to3/pgen2/tokenize.py
tests/data/string_prefixes.py

index 565c36f8c6099d746c55500801a54193109b2570..5a8a0ef9f7c4bb8caee688929b40e1ec5c09c695 100644 (file)
@@ -28,6 +28,7 @@
   `--target-version` is set to 3.10 and higher). (#2728)
 - Fix handling of standalone `match()` or `case()` when there is a trailing newline or a
   comment inside of the parentheses. (#2760)
+- Black now normalizes string prefix order (#2297)
 
 ### Packaging
 
index 68dff3eef3f0469e73ae85ed26389c6d9248bfa3..11fe2c8cceb6fe84b8690b102b73fb410328090e 100644 (file)
@@ -233,10 +233,10 @@ _Black_ prefers double quotes (`"` and `"""`) over single quotes (`'` and `'''`)
 will replace the latter with the former as long as it does not result in more backslash
 escapes than before.
 
-_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.
+_Black_ also standardizes string prefixes. Prefix characters are made lowercase with the
+exception of [capital "R" prefixes](#rstrings-and-rstrings), unicode literal markers
+(`u`) are removed because they are meaningless in Python 3, and in the case of multiple
+characters "r" is put first as in spoken language: "raw f-string".
 
 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
index 262c2ba4313bee09839008f59bf49d4d26543dc6..9d0e2eb8430e538efcba96582dcfc9ee2d4bc468 100644 (file)
@@ -149,6 +149,10 @@ def normalize_string_prefix(s: str) -> str:
         .replace("U", "")
         .replace("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():
+        new_prefix = new_prefix[::-1]
     return f"{new_prefix}{match.group(2)}"
 
 
index a7e17df1e8f9f9452ae3e49db895fbc875ab40bd..257dbef4a194b60ef81eabc22cd373891abec232 100644 (file)
@@ -293,7 +293,7 @@ class Untokenizer:
 
 
 cookie_re = re.compile(r"^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)", re.ASCII)
-blank_re = re.compile(br"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII)
+blank_re = re.compile(rb"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII)
 
 
 def _get_normal_name(orig_enc: str) -> str:
index 9ddc2b540fcefe3c729e198cdc9e6ba5da5f4861..f86da696e1566482850877c8ca3d1ed745ed0647 100644 (file)
@@ -1,10 +1,13 @@
-#!/usr/bin/env python3.6
+#!/usr/bin/env python3
 
-name = R"Łukasz"
-F"hello {name}"
-B"hello"
-r"hello"
-fR"hello"
+name = "Łukasz"
+(f"hello {name}", F"hello {name}")
+(b"", B"")
+(u"", U"")
+(r"", R"")
+
+(rf"", fr"", Rf"", fR"", rF"", Fr"", RF"", FR"")
+(rb"", br"", Rb"", bR"", rB"", Br"", RB"", BR"")
 
 
 def docstring_singleline():
@@ -20,13 +23,16 @@ def docstring_multiline():
 # output
 
 
-#!/usr/bin/env python3.6
+#!/usr/bin/env python3
+
+name = "Łukasz"
+(f"hello {name}", f"hello {name}")
+(b"", b"")
+("", "")
+(r"", R"")
 
-name = R"Łukasz"
-f"hello {name}"
-b"hello"
-r"hello"
-fR"hello"
+(rf"", rf"", Rf"", Rf"", rf"", rf"", Rf"", Rf"")
+(rb"", rb"", Rb"", Rb"", rb"", rb"", Rb"", Rb"")
 
 
 def docstring_singleline():