msg81931 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2009-02-13 14:12 |
>>> "{0:f}".format(2.5) '2.500000' >>> "{0:f}".format("spam") Traceback (most recent call last): File "", line 1, in 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) *  |
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) *  |
Date: 2009-02-13 21:35 |
Seems like a reasonable backport. |
|
|
msg82167 - (view) |
Author: Eric V. Smith (eric.smith) *  |
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) *  |
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 "", line 1, in 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) *  |
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) *  |
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 "", line 1, in ValueError: Unknown format code 'x' for object of type 'str' >>> |
|
|
msg82527 - (view) |
Author: Eric V. Smith (eric.smith) *  |
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) *  |
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) *  |
Date: 2009-02-20 14:31 |
Committed in: r69806 (trunk) r69807 (release26-maint) r69808 (py3k) r69809 (release30-maint) |
|
|