Skip to content

Rewrite format literals containing \N escapes#281

Closed
gvangool wants to merge 9 commits intoasottile:masterfrom
gvangool:issue-280
Closed

Rewrite format literals containing \N escapes#281
gvangool wants to merge 9 commits intoasottile:masterfrom
gvangool:issue-280

Conversation

@gvangool
Copy link
Copy Markdown

This is my attempt at fixing #280 (I started it for #278).

It's based on current parser behaviour:

  • for the % encoding we let it convert to \N{{snowman}} and fix it afterwards
  • for the .format, the source will end with \N (with name being snowman). We can reuse the original format for this.

Copy link
Copy Markdown
Owner

@asottile asottile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a couple edge cases -- definitely works for the cases you've proposed though 👍

Comment thread pyupgrade.py Outdated
Comment thread pyupgrade.py Outdated
@gvangool gvangool requested a review from asottile April 12, 2020 23:28
Comment thread tests/fstrings_test.py Outdated
Comment thread pyupgrade.py Outdated
Comment thread pyupgrade.py Outdated
@asottile
Copy link
Copy Markdown
Owner

looks good, looks like there's one last thing to fix 🤔

diff --git a/tests/format_literals_test.py b/tests/format_literals_test.py
index f3610a1..a809f74 100644
--- a/tests/format_literals_test.py
+++ b/tests/format_literals_test.py
@@ -49,7 +49,7 @@ def test_intentionally_not_round_trip(s, expected):
         "'{' '0}'.format(1)",
         # comment looks like placeholder but is not!
         '("{0}" # {1}\n"{2}").format(1, 2, 3)',
-        # TODO: this works by accident (extended escape treated as placeholder)
+        # this works by accident (extended escape treated as placeholder)
         r'"\N{snowman} {}".format(1)',
     ),
 )
@@ -99,6 +99,8 @@ def test_format_literals_noop(s):
         ),
         # parenthesized string literals
         ('("{0}").format(1)', '("{}").format(1)'),
+        # extended unicode escapes
+        (r'"\N{snowman} {0}".format(1)', r'"\N{snowman} {}".format(1)'),
     ),
 )
 def test_format_literals(s, expected):

Comment thread pyupgrade.py Outdated
Comment thread pyupgrade.py
Comment thread pyupgrade.py
Comment on lines +2450 to +2454
try:
tokens[i] = token._replace(src=_to_fstring(token.src, node))
del tokens[i + 1:end + 1]
except KeyError:
pass
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for an unparse-like function that could be used in the _to_fstring because this feels like it's handling an implementation detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants