classification
Title: Confusing error messages in str.format()
Type: behavior Stage: patch review
Components: Interpreter Core, Unicode Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, ezio.melotti, iritkatriel, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-09-14 11:12 by serhiy.storchaka, last changed 2021-01-13 19:42 by iritkatriel.

Pull Requests
URL Status Linked Edit
PR 24213 open iritkatriel, 2021-01-13 19:40
Messages (3)
msg276415 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-09-14 11:12
For example:

>>> '{:04}'.format('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '=' alignment not allowed in string format specifier

There is no any '=' in the format string.

>>> '{: }'.format('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Sign not allowed in string format specifier

There is no any sign ('+' or '-') in the format string.

There also an inconsistency between the wording of "something not allowed in string format specifier" and:

>>> '{:x}'.format('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'x' for object of type 'str'
>>> '{:xx}'.format('abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
msg276417 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-09-14 11:32
See also issue 27772.

The difference in the error messages is due to the first ones looking for specific invalid combinations (in this case things the string formatter does not understand), and the last one which is "I have no idea what you're asking for".
msg385052 - (view) Author: Irit Katriel (iritkatriel) * (Python triager) Date: 2021-01-13 19:42
My PR fixes the second case ('{: }') and Serhiy's PR on Issue27772 fixes the first case ('{:04}').
History
Date User Action Args
2021-01-13 19:42:01iritkatrielsetmessages: + msg385052
2021-01-13 19:40:50iritkatrielsetkeywords: + patch
nosy: + iritkatriel

pull_requests: + pull_request23040
stage: patch review
2016-09-14 11:32:11eric.smithsettype: behavior

messages: + msg276417
nosy: + eric.smith
2016-09-14 11:12:52serhiy.storchakasettitle: Confusing error examples in str.format() -> Confusing error messages in str.format()
2016-09-14 11:12:35serhiy.storchakacreate