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: Unhelpful error message with str.format()
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: LambertDW, eric.smith, pitrou, rhettinger
Priority: normal Keywords: patch

Created on 2009-02-13 14:12 by pitrou, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue5247.patch eric.smith, 2009-02-15 21:06
issue5247-1.patch eric.smith, 2009-02-16 00:13
Messages (10)
msg81931 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-02-13 14:12
>>> "{0:f}".format(2.5)
'2.500000'
>>> "{0:f}".format("spam")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown conversion type f

The error message should mention the type of the argument that doesn't
support the given conversion type (e.g. "Unknown conversion type f for
object of type 'float'"). Otherwise it can be very confusing.
msg81969 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-02-13 20:04
I agree. I'm not sure on backporting this.

I'll work on a patch.
msg81973 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-02-13 21:35
Seems like a reasonable backport.
msg82167 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-02-15 20:40
I've gone back and read PEP 3101. To use its terminology, I think the
error message should be something like:
Unknown presentation type %c for type %s.

I'm not sure where I got the original wording "conversion type". It's
true that it's sometimes used for type conversion (int->float, for
example), but that's not its real purpose.

It's unfortunate that "type" is used in the PEP and the online docs for
"presentation type", and we're trying to report an error on the
argument's type.

Any suggestions for clearer wording?
msg82169 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-02-15 21:06
The attached patch (against trunk) changes the message.

However, it has at least one unintended consequence. If you have an
object with no __format__, it gets converted to a string, which is then
formatted. So you get:

>>> '{0:^10}'.format(0j)
'    0j    '
>>> '{0:^10x}'.format(0j)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown presentation type x for str
>>> 

I'm calling format on an complex number, but the error says "str". The
error is correct, because the complex number has been converted to a
string before the formatting mechanism can get the actual type. I think
we'll have to live with this if we add the type to the error message.
msg82170 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-02-15 21:11
> Any suggestions for clearer wording?

Use "formatting code" rather than "presentation type"?
msg82192 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-02-16 00:13
With this patch, I changed it to "format code", and made it more in line
with Antoine's original suggested message.

I'm okay with "format code" or "formatting code", but if we do use
either of those wordings, we should change the documentation to match.
Not sure if the PEP should be modified. It's slightly out of date anyway.

>>> '{0:x}'.format('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'x' for object of type 'str'
>>>
msg82527 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-02-20 12:24
I'm getting ready to commit this. Does it need a test? I poked around
and didn't see any tests that validate exception text, but maybe there
are some I missed. I tend to think it shouldn't have a test, but I'm not
sure what the norm is.
msg82528 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-02-20 12:31
Looks good to me. I don't think you need to write a specific test for this.
msg82531 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-02-20 14:31
Committed in:
r69806 (trunk)
r69807 (release26-maint)
r69808 (py3k)
r69809 (release30-maint)
History
Date User Action Args
2022-04-11 14:56:45adminsetgithub: 49497
2009-02-20 14:31:09eric.smithsetstatus: open -> closed
resolution: accepted
messages: + msg82531
stage: resolved
2009-02-20 12:31:43pitrousetmessages: + msg82528
2009-02-20 12:24:39eric.smithsetmessages: + msg82527
2009-02-16 00:13:01eric.smithsetfiles: + issue5247-1.patch
messages: + msg82192
2009-02-15 21:11:17pitrousetmessages: + msg82170
2009-02-15 21:06:33eric.smithsetkeywords: + patch
files: + issue5247.patch
messages: + msg82169
2009-02-15 20:41:01eric.smithsetmessages: + msg82167
2009-02-14 01:32:30eric.smithsetassignee: eric.smith
2009-02-13 21:35:07rhettingersetnosy: + rhettinger
messages: + msg81973
2009-02-13 20:04:43eric.smithsetmessages: + msg81969
2009-02-13 19:46:07benjamin.petersonsetnosy: + eric.smith
2009-02-13 14:56:28LambertDWsetnosy: + LambertDW
2009-02-13 14:12:54pitroucreate