[docs] [issue13790] In str.format an incorrect error message for list, tuple, dict, set

Eric V. Smith report at bugs.python.org
Sat Jan 21 01:16:25 CET 2012


Eric V. Smith <eric at trueblade.com> added the comment:

I don't think "{}" is the correct way to document this. These all have an empty format specifier:

"{}".format(foo)
"{:}".format(foo)
"{0}".format(foo)
"{0:}".format(foo)
"{name}".format(name=foo)
format(foo, "")
format(foo)

That is, they all call foo.__format__(""). If foo.__format__ (well, really type(foo).__format__) doesn't exist, then object.__format__(foo, "") gets called. It's object.__format__ that's checking for the empty format string, and if so it returns str(foo).

What would you suggest changing the ':d' error message to, for objects that don't support a format type of 'd'? This makes sense to me:

>>> format('', 'd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'd' for object of type 'str'

The problem, if there is one, is:
>>> format([], 'd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'd' for object of type 'str'

The problem is that the str that's producing this error doesn't know that it exists because object.__format__ returned str([]).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13790>
_______________________________________


More information about the docs mailing list