This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: f-strings: format spec should not accept unicode escapes
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Decorater, eric.smith, gvanrossum, martin.panter, serhiy.storchaka, yselivanov
Priority: normal Keywords:

Created on 2016-11-29 00:31 by yselivanov, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)
msg281924 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-11-29 00:31
Right now, Python 3.6b4 happily accepts the following:

    >>> f'{10:02\N{LATIN CAPITAL LETTER X}}'
    '0A'
    >>> f'{10:02X}'
    '0A'

I think that the first line should not be accepted (as we now don't accept escaped open curly brace).

At least this should be documented in the PEP as a thing that shouldn't be relied upon, i.e. something that might not be supported in the future versions.
msg281925 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-11-29 00:37
Also this inconsistency: we already don't accept escaped letters after `!`:

>>> f'{min!\N{LATIN SMALL LETTER R}}'
  File "<stdin>", line 1
SyntaxError: f-string: invalid conversion character: expected 's', 'r', or 'a'
msg281927 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-11-29 01:11
I don’t have Py 3.6 to test on, but won’t that make it unnecessarily inconvenient to use certain format codes? I.e. codes that involve non-ASCII characters, control codes, quote signs, or backslashes. Slightly silly use case:

>>> "The time is {:%I\N{DEGREE SIGN} %M\N{PRIME} %S\N{DOUBLE PRIME} %p}".format(datetime.datetime.now())
'The time is 01° 07′ 41″ AM'
msg281928 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-11-29 01:13
I think this is documented. This is a result of supported nested expressions in the format_spec:

>>> an_x = "X"
>>> f'{10:02{an_x}}'
'0A'
msg281929 - (view) Author: Decorater (Decorater) * Date: 2016-11-29 01:23
there is also the most common.
'{10:02{0}}'.format(an_x)
msg281930 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-11-29 01:24
Specifically, see: https://www.python.org/dev/peps/pep-0498/#format-specifiers

str.format() also works this way:
>>> '{0:02{1}}'.format(10, an_x)
'0A'
msg296469 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-06-20 16:33
Should this issue be closed as "not a bug"?
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73013
2017-11-09 18:02:17serhiy.storchakasetstatus: pending -> closed
resolution: not a bug
stage: resolved
2017-06-20 16:33:40serhiy.storchakasetstatus: open -> pending

messages: + msg296469
2016-11-29 01:24:24eric.smithsetmessages: + msg281930
2016-11-29 01:23:30Decoratersetnosy: + Decorater
messages: + msg281929
2016-11-29 01:13:51eric.smithsetmessages: + msg281928
2016-11-29 01:11:51martin.pantersetnosy: + martin.panter
messages: + msg281927
2016-11-29 00:37:06yselivanovsetmessages: + msg281925
2016-11-29 00:31:37yselivanovcreate