Issue 13790: In str.format an incorrect error message for list, tuple, dict, set (original) (raw)
Created on 2012-01-15 05:31 by py.user, last changed 2022-04-11 14:57 by admin. This issue is now closed.
Messages (34)
Author: py.user (py.user) *
Date: 2012-01-15 05:31
'{0:d}'.format('a') Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'str' '{0:d}'.format(1+1j) Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'complex' '{0:d}'.format([]) Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'str'
also strange behavior:
'{0:s}'.format((1, 2, 3)) '(1, 2, 3)' '{0:10.5s}'.format([1, 2, 3]) '[1, 2 '
Author: Eric V. Smith (eric.smith) *
Date: 2012-01-15 05:57
I agree it's not the best error message. What's happening is that these types (list, tuple, etc.) do not implement format, so object.format is used. It returns str(self). Then the resulting string is formatted with the given format_spec. Since str does not support the 'd' format type, the error you see is raised.
I'm open to suggestions on how to improve this, but I don't see how it's possible given what str.format knows when it generates the error.
Author: py.user (py.user) *
Date: 2012-01-15 22:46
also strange(unobvious) behavior:
'{0:.3s}'.format((i for i in (1, 2, 3))) '<ge' '{0:.3s}'.format(range(10)) 'ran' '{0:.3s}'.format(None) 'Non'
it would be better to print an error: ValueError: Unknown format code 's' for object of type 'generator'
like in this:
'{0:d}'.format(4.5) Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'float'
in the documentation there is nothing about it
Author: R. David Murray (r.david.murray) *
Date: 2012-01-15 22:54
No, it wouldn't. I expect
"{}".format(x)
to produce something for an arbitrary x. Breaking that would break a fundamental Python contract.
Improving the error message for 'd' is more possible. Perhaps "the format code 'd' is not implemented by objects of type "?
Author: R. David Murray (r.david.murray) *
Date: 2012-01-15 23:00
Oh, and when you say there is nothing in the documentation about the 's' case for arbitrary objects, it is made clear in various places that every object has an str, which defaults to its repr if it has no specific str. Combine that with the description of what happens when you use a fixed field length for 's', and you get the results you see. There should be nothing surprising about this to anyone who has read the tutorial, I think. (But specific suggestions for improving the docs are always welcome.)
Author: py.user (py.user) *
Date: 2012-01-15 23:26
R. David Murray wrote:
it is made clear in various places that every object has an str
here: http://docs.python.org/py3k/library/string.html#format-specification-mini-language
3rd paragraph: "A general convention is that an empty format string ("") produces the same result as if you had called str() on the value. A non-empty format string typically modifies the result."
"an empty format string ("")" what does it mean ?
"".format(value) or "{}".format(value) or "{0}".format(value) ?
Author: py.user (py.user) *
Date: 2012-01-15 23:29
also here: http://docs.python.org/py3k/library/string.html#format-examples
there is no example with list or tuple to know exactly how they are formatted
Author: R. David Murray (r.david.murray) *
Date: 2012-01-15 23:49
"an empty format string" is exactly what I was talking about. Putting nothing between the {}'s is an empty format string. I can't think of any way to make that wording clearer.
The format docs should not contains examples of the repr of all possible python objects. The examples of what tuples and lists and dicts &c look like are shown in the docs for those objects.
Author: py.user (py.user) *
Date: 2012-01-16 04:25
R. David Murray wrote:
Putting nothing between the {}'s is an empty format string.
this is an empty replacement field
here: http://docs.python.org/py3k/library/string.html#format-string-syntax
the definition of format string: "Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output."
"The grammar for a replacement field is as follows:" replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
Author: R. David Murray (r.david.murray) *
Date: 2012-01-16 12:53
Good point. That should be fixed. It should be "empty format specification".
Author: Eric V. Smith (eric.smith) *
Date: 2012-01-16 16:19
Changing to a documentation issue.
Author: Terry J. Reedy (terry.reedy) *
Date: 2012-01-20 23:44
Doc patch attached to make sure correct. Should {} be quoted?
Eric, do you want to close off the idea of changing :d errors, or switch back after the doc fix?
Author: Eric V. Smith (eric.smith) *
Date: 2012-01-21 00:16
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([]).
Author: R. David Murray (r.david.murray) *
Date: 2012-01-21 04:48
Oh, I see. Yes, that is a problem.
object.format knows the type of the object it was called on, right? Couldn't it catch the error and re-raise it with the correct type? (If the type isn't str, of course, we don't want to get into an infinite recursion.)
Author: R. David Murray (r.david.murray) *
Date: 2012-01-21 04:50
Oh, never mind that comment about recursion, I wasn't thinking it through.
Author: Terry J. Reedy (terry.reedy) *
Date: 2012-01-21 05:56
OK, the example of an empty format spec should be dropped. Let people figure it out ;-).
format([], 'd') Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'str'
One possibility is to give (str of) the object instead of the type:
ValueError: Unknown format code 'd' for object '[]'
The downside is a long message for long strings. It would need to be limited (as is done in test error reports).
Author: Eric V. Smith (eric.smith) *
Date: 2012-01-21 09:47
While looking at object.format, I recall that we've already addressed this, sort of. For a different reason, this is already deprecated in 3.3 and will become an error in 3.4. See issues 9856 and 7994.
$ ./python -Wd Python 3.3.0a0 (default:40e1be1e0707, Jan 15 2012, 00:58:51) [GCC 4.6.1] on linux Type "help", "copyright", "credits" or "license" for more information.
format([], 'd') main:1: DeprecationWarning: object.format with a non-empty format string is deprecated Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'str' [67288 refs]
We could still have object.format catch and re-throw the ValueError with a better message. I'd have to think it through if we could catch all ValueErrors, or if it's possible for another ValueError to be thrown and we'd only catch and rethrow this specific ValueError.
But since this is deprecated, I'm not sure it's worth the hassle. I'd advocate closing this issue as "won't fix".
Author: R. David Murray (r.david.murray) *
Date: 2012-01-21 14:20
So the error is going to be something about the source type not supporting 'format'?
That change will also address the OP's concern about truncated reprs when a fixed string length is specified, so I agree that the title issue can be closed. Terry's patch with the ("{}") removed should be committed, though.
Author: Eric V. Smith (eric.smith) *
Date: 2012-01-21 17:35
The error message will be: "non-empty format string passed to object.format".
I agree with your comment about Terry's patch.
Author: Terry J. Reedy (terry.reedy) *
Date: 2012-01-22 02:16
Looking further, I noticed that 'string' needed to be changed to 'specification' in the following sentence also. Then I decided that the preceding sentence
"Most built-in types implement the following options for format specifications, although some of the formatting options are only supported by the numeric types."
should really follow the one about non-empty format specs. This positioning should make it more obvious that most of the options affect the string representation of the object after, not before, the string is produced, and are therefore applicable to all objects and not just string and number objects. I also propose to modify it so it is shorter and no longer contradictory, to read
"Most built-in types implement various options for such modifications, although some are only supported by the numeric types."
Further on, under "The available string presentation types are:"
I think "'s'
String format. This is the default type for strings and may be omitted." should have 'and other non-numeric types ' inserted after strings. New patch i13790b.diff attached
The point of these additional changes is to make it clearer that the default formatting of non-number, non-string objects is to call str() and then apply the options to the resulting string. That makes something like
format(range(5), '-^20s') # same with object.format(), 3.3.0a0 '----range(0, 5)-----' predictable and comprehensible.
I agree with not making a temporary change (but see below ;-).
But it seems that the 3.4 message should at least be "numeric format string passed to object.format" or "format string with number-only options passed to object.format" or "object.format cannot handle number-only options" as string formats work fine and, I presume, are not deprecated (?).
However, if the new ValueError message did not specify object.format (which could still be confusing, even if more accurate), the change could be make now. For instance 'Numeric option 'd' for non-number object'. It would not really matter if it is later raised in object.format instead of str.format. I believe all of the format codes 'unknown' to str (and by extension, by default, to all other non-number types) are number codes.
Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2012-06-29 18:17
'%d' % ([],) Traceback (most recent call last): File "", line 1, in TypeError: %d format: a number is required, not list
Author: Eric V. Smith (eric.smith) *
Date: 2012-06-29 18:29
Serhiy: I'm not sure what you're saying. At the point that str.format() is producing its error message, it doesn't know as much as %-formatting does about the original arguments, so it can't produce a similar message.
Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2012-06-30 07:01
Serhiy: I'm not sure what you're saying. At the point that str.format() is producing its error message, it doesn't know as much as %-formatting does about the original arguments, so it can't produce a similar message.
I'm surprised that the code of the classic and the modern formatting is so different. Looking deeper, I saw that the issue will go away in 3.4. I agree with you in .
Author: Mark Lawrence (BreamoreBoy) *
Date: 2014-06-13 00:04
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
'{0:d}'.format('a') Traceback (most recent call last): File "<pyshell#0>", line 1, in '{0:d}'.format('a') ValueError: Unknown format code 'd' for object of type 'str'
Nothing appears to have changed despite "the issue will go away in 3.4" in . What should have happened here?
Author: Eric V. Smith (eric.smith) *
Date: 2014-06-13 00:32
I believe that comment was referring to the subject of this bug:
$ ./python Python 3.4.1+ (3.4:bec6f18dd636, Jun 12 2014, 20:23:30) [GCC 4.8.1] on linux Type "help", "copyright", "credits" or "license" for more information.
format([], 'd') Traceback (most recent call last): File "", line 1, in TypeError: non-empty format string passed to object.format format((), 'd') Traceback (most recent call last): File "", line 1, in TypeError: non-empty format string passed to object.format format({}, 'd') Traceback (most recent call last): File "", line 1, in TypeError: non-empty format string passed to object.format format(set(), 'd') Traceback (most recent call last): File "", line 1, in TypeError: non-empty format string passed to object.format
With the possible exception of listing the type in this error message, I think these are all fine.
I'm not sure what you'd expect format('a', 'd') to produce other than the error you're seeing. 'd' is in fact an unknown "format code" for str.
format('a', 'd') Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'str'
Author: py.user (py.user) *
Date: 2014-06-13 01:21
Python 2.7.7 is still printing.
format([], 'd') Traceback (most recent call last): File "", line 1, in ValueError: Unknown format code 'd' for object of type 'str'
Author: Terry J. Reedy (terry.reedy) *
Date: 2014-06-13 01:24
Yes, the deprecation in 3.3 did not apply to 2.7.
Author: Cheryl Sabella (cheryl.sabella) *
Date: 2018-02-25 17:05
From the examples in , changed it to print the object type in the message.
format([], 'd') Traceback (most recent call last): File "", line 1, in TypeError: unsupported format string passed to list.format format((), 'd') Traceback (most recent call last): File "", line 1, in TypeError: unsupported format string passed to tuple.format
Would the change left on this issue be to create a PR for Terry's documetation patch?
Thanks!
Author: Terry J. Reedy (terry.reedy) *
Date: 2018-02-25 19:14
In , R. David Murry said "Terry's [first] patch with the ("{}") removed should be committed, though." In , Eric V. Smith said "I agree with your comment about Terry's patch."
My second patch removed "{}" but also made more text changes, explained in . Someone should re-review
Author: Terry J. Reedy (terry.reedy) *
Date: 2020-02-28 19:46
PR-18690 makes the approved change of 'string' to 'specification'. After merging, I will re-review the other changes in i13790b.diff and possibly make another PR for review.
Author: Terry J. Reedy (terry.reedy) *
Date: 2020-02-28 19:59
New changeset 916895f93905f8b8dad677cceff501833f5a633a by Terry Jan Reedy in branch 'master': bpo-13790: Change 'string' to 'specification' in format doc (GH-18690) https://github.com/python/cpython/commit/916895f93905f8b8dad677cceff501833f5a633a
Author: miss-islington (miss-islington)
Date: 2020-02-28 20:04
New changeset 5157506e043f75f49caecae1c6afee8517a7bbb0 by Miss Islington (bot) in branch '3.7': bpo-13790: Change 'string' to 'specification' in format doc (GH-18690) https://github.com/python/cpython/commit/5157506e043f75f49caecae1c6afee8517a7bbb0
Author: miss-islington (miss-islington)
Date: 2020-02-28 20:07
New changeset 445152e0d3ab6e4381aef8d1404c2c17a516070f by Miss Islington (bot) in branch '3.8': bpo-13790: Change 'string' to 'specification' in format doc (GH-18690) https://github.com/python/cpython/commit/445152e0d3ab6e4381aef8d1404c2c17a516070f
Author: Irit Katriel (iritkatriel) *
Date: 2020-09-27 14:03
This seems complete.
History
Date
User
Action
Args
2022-04-11 14:57:25
admin
set
github: 57999
2020-09-27 14:25:01
eric.smith
set
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-09-27 14:03:42
iritkatriel
set
nosy: + iritkatriel
messages: +
2020-02-28 20:07:00
miss-islington
set
messages: +
2020-02-28 20:04:25
miss-islington
set
messages: +
2020-02-28 20:01:22
miss-islington
set
pull_requests: + <pull%5Frequest18054>
2020-02-28 19:59:30
miss-islington
set
nosy: + miss-islington
pull_requests: + <pull%5Frequest18052>
2020-02-28 19:59:23
terry.reedy
set
messages: +
2020-02-28 19:46:46
terry.reedy
set
messages: +
versions: + Python 3.9, - Python 2.7, Python 3.6
2020-02-28 19:41:27
terry.reedy
set
pull_requests: + <pull%5Frequest18049>
2018-02-26 15:37:03
BreamoreBoy
set
nosy: - BreamoreBoy
2018-02-25 19:14:53
terry.reedy
set
messages: +
2018-02-25 17:05:08
cheryl.sabella
set
nosy: + cheryl.sabella
messages: +
versions: + Python 3.6, Python 3.7, Python 3.8, - Python 3.4, Python 3.5
2014-06-13 01:24:57
terry.reedy
set
stage: test needed -> patch review
2014-06-13 01:24:04
terry.reedy
set
messages: +
versions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
2014-06-13 01:21:30
py.user
set
messages: +
2014-06-13 00:32:12
eric.smith
set
messages: +
2014-06-13 00:04:39
BreamoreBoy
set
nosy: + BreamoreBoy
messages: +
2013-01-07 16:10:40
serhiy.storchaka
set
nosy: - serhiy.storchaka
2012-06-30 07:01:09
serhiy.storchaka
set
messages: +
2012-06-29 18:29:26
eric.smith
set
messages: +
2012-06-29 18:17:31
serhiy.storchaka
set
nosy: + serhiy.storchaka
messages: +
2012-05-04 18:29:10
ezio.melotti
set
nosy: + ezio.melotti
2012-05-04 18:25:14
eric.smith
link
2012-01-22 02:16:17
terry.reedy
set
files: + i13790b.diff
assignee: docs@python -> terry.reedy
messages: +
2012-01-21 17:35:19
eric.smith
set
messages: +
2012-01-21 14:20:11
r.david.murray
set
messages: +
2012-01-21 09:47:18
eric.smith
set
messages: +
2012-01-21 05:56:24
terry.reedy
set
messages: +
2012-01-21 04:50:15
r.david.murray
set
messages: +
2012-01-21 04:48:37
r.david.murray
set
messages: +
2012-01-21 00:16:21
eric.smith
set
messages: +
2012-01-20 23:44:42
terry.reedy
set
files: + i13790.diff
nosy: + terry.reedy
messages: +
keywords: + patch
stage: test needed
2012-01-16 16:19:26
eric.smith
set
assignee: eric.smith -> docs@python
components: + Documentation, - Interpreter Core
versions: + Python 2.7
keywords: + easy
nosy: + docs@python
messages: +
2012-01-16 12:53:04
r.david.murray
set
messages: +
2012-01-16 04:25:47
py.user
set
messages: +
2012-01-15 23:49:48
r.david.murray
set
messages: +
2012-01-15 23:29:52
py.user
set
messages: +
2012-01-15 23:26:52
py.user
set
messages: +
2012-01-15 23:00:49
r.david.murray
set
messages: +
2012-01-15 22:54:15
r.david.murray
set
nosy: + r.david.murray
messages: +
2012-01-15 22:46:03
py.user
set
messages: +
2012-01-15 05:57:34
eric.smith
set
versions: + Python 3.2, Python 3.3, - Python 3.1
nosy: + eric.smith
messages: +
assignee: eric.smith
2012-01-15 05:31:18
py.user
create