classification
Title: Confusing error messages in str.format()
Type: behavior Stage: resolved
Components: Interpreter Core, Unicode Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, ezio.melotti, iritkatriel, miss-islington, pitrou, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-09-14 11:12 by serhiy.storchaka, last changed 2021-05-14 08:08 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24213 merged iritkatriel, 2021-01-13 19:40
PR 26110 closed miss-islington, 2021-05-13 20:57
PR 26111 merged miss-islington, 2021-05-13 20:57
PR 26112 merged miss-islington, 2021-05-13 20:57
Messages (7)
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 committer) Date: 2021-01-13 19:42
My PR fixes the second case ('{: }') and Serhiy's PR on Issue27772 fixes the first case ('{:04}').
msg393608 - (view) Author: miss-islington (miss-islington) Date: 2021-05-13 21:25
New changeset 2d780237d95cd3d95401f52be2edeac8b458eb68 by Miss Islington (bot) in branch '3.10':
bpo-28146: Fix a confusing error message in str.format() (GH-24213)
https://github.com/python/cpython/commit/2d780237d95cd3d95401f52be2edeac8b458eb68
msg393610 - (view) Author: miss-islington (miss-islington) Date: 2021-05-13 21:35
New changeset 133013e8a1ecd570266de766e2df9745a24343be by Miss Islington (bot) in branch '3.9':
bpo-28146: Fix a confusing error message in str.format() (GH-24213)
https://github.com/python/cpython/commit/133013e8a1ecd570266de766e2df9745a24343be
msg393612 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2021-05-13 21:39
It seems like this issue is entirely fixed now, closing.
msg393631 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-05-14 08:08
commit 4aeee0b47b3a2b604bbac37040320ffc88c291f2
Author: Irit Katriel <iritkatriel@yahoo.com>
Date:   Thu May 13 21:55:55 2021 +0100

    bpo-28146: Fix a confusing error message in str.format() (GH-24213)
    
    
    
    Automerge-Triggered-By: GH:pitrou
History
Date User Action Args
2021-05-14 08:08:15vstinnersetmessages: + msg393631
2021-05-13 21:39:16pitrousetstatus: open -> closed

versions: + Python 3.9, Python 3.10
nosy: + pitrou

messages: + msg393612
resolution: fixed
stage: patch review -> resolved
2021-05-13 21:35:40miss-islingtonsetmessages: + msg393610
2021-05-13 21:25:08miss-islingtonsetmessages: + msg393608
2021-05-13 20:57:19miss-islingtonsetpull_requests: + pull_request24756
2021-05-13 20:57:14miss-islingtonsetpull_requests: + pull_request24755
2021-05-13 20:57:08miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24754
2021-05-13 20:36:26pitrousetversions: + Python 3.11
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