]> 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:

Fix string normalization sometimes producing invalid fstrings (#327)
authorZsolt Dollenstein <zsol.zsol@gmail.com>
Sat, 9 Jun 2018 19:30:49 +0000 (21:30 +0200)
committerŁukasz Langa <lukasz@langa.pl>
Sat, 9 Jun 2018 19:30:49 +0000 (12:30 -0700)
README.md
black.py
tests/data/fstring.py

index 7ae640cb4aefcd976679801d5a2ad17997838e46..533a1f82f89e746b7b6540908e44de7215313e04 100644 (file)
--- a/README.md
+++ b/README.md
@@ -807,6 +807,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 
 ## Change Log
 
+### 18.6b3
+
+* fixed improper formatting of f-strings with quotes inside interpolated
+  expressions (#322)
+
 ### 18.6b2
 
 * added `--config` (#65)
index 6db661c525042b44cd69376c7cda6978caaa82fc..35daaa9d634fa0d0a278014df7277ef56d1af1fc 100644 (file)
--- a/black.py
+++ b/black.py
@@ -2549,6 +2549,12 @@ def normalize_string_quotes(leaf: Leaf) -> None:
             leaf.value = 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)
+    if "f" in prefix.casefold():
+        matches = re.findall(r"[^{]\{(.*?)\}[^}]", new_body)
+        for m in matches:
+            if "\\" in str(m):
+                # Do not introduce backslashes in interpolated expressions
+                return
     if new_quote == '"""' and new_body[-1:] == '"':
         # edge case:
         new_body = new_body[:-1] + '\\"'
index b288cbc413003446a1c3c0e82e2616a873b9ae95..4b33231c01c08701c1f2fa47de9cfe8a1f0388c9 100644 (file)
@@ -1,5 +1,21 @@
 f"f-string without formatted values is just a string"
 f"{{NOT a formatted value}}"
+f"{{NOT 'a' \"formatted\" \"value\"}}"
 f"some f-string with {a} {few():.2f} {formatted.values!r}"
-f"{f'{nested} inner'} outer"
+f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
+f"{f'''{'nested'} inner'''} outer"
+f"\"{f'{nested} inner'}\" outer"
 f"space between opening braces: { {a for a in (1, 2, 3)}}"
+f'Hello \'{tricky + "example"}\''
+
+# output
+
+f"f-string without formatted values is just a string"
+f"{{NOT a formatted value}}"
+f'{{NOT \'a\' "formatted" "value"}}'
+f"some f-string with {a} {few():.2f} {formatted.values!r}"
+f'some f-string with {a} {few(""):.2f} {formatted.values!r}'
+f"{f'''{'nested'} inner'''} outer"
+f"\"{f'{nested} inner'}\" outer"
+f"space between opening braces: { {a for a in (1, 2, 3)}}"
+f'Hello \'{tricky + "example"}\''