Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform rewrite of format literals containing \N escapes #280

Closed
asottile opened this issue Apr 12, 2020 · 2 comments · Fixed by #542
Closed

Perform rewrite of format literals containing \N escapes #280

asottile opened this issue Apr 12, 2020 · 2 comments · Fixed by #542

Comments

@asottile
Copy link
Owner

#279 prevents the crash / incorrect behaviour from #278, but this should be supported -- shouldn't be too difficult but will likely need to reimplement the format string parsing (the stdlib one doesn't notice these escape sequences, probably because they're gone by that point of the parser)

The current behaviours that should be fixed:

  1. % format: '\N{snowman} %s' % (a,) should become '\N{snowman} {}'.format(a)
  2. f-string: '\N{snowman} {}'.format(a) should become f'\N{snowman} {a}'
@peterjc
Copy link

peterjc commented Sep 9, 2021

To anyone else wondering if the second example is wrong, with a literal {snowman} inside an f-string, the examples all do this:

>>> a = "melts"
>>> '\N{snowman} %s' % (a,)
'☃ melts'
>>> '\N{snowman} {}'.format(a)
'☃ melts'
>>> f'\N{snowman} {a}'
'☃ melts'

The escape sequence \N{name} means the character in the Unicode database https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

But nowadays you could just write ☃ directly in the Python code:

>>> a = "melts"
>>> f'☃ {a}'

I guess pyupgrade isn't going to replace escaped emoji with literal emoji, is it?

@asottile
Copy link
Owner Author

asottile commented Sep 9, 2021

there are situations where that is undesirable:

>>> '\N{GREEK CAPITAL LETTER ALPHA}' == 'A'
False
>>> 'Α' == 'A'
False

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

Successfully merging a pull request may close this issue.

2 participants