Message 151711 - Python tracker (original) (raw)

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 "", line 1, in 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 "", line 1, in 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([]).