msg87394 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2009-05-07 19:31 |
String Services / Format Specification Mini-Language (7.1.3.1 in 3.1) "The precision is ignored for integer values." in 3.0.1 and 3.1.b1 and, I presume in 2.6/7 doc should be "A precision is not allowed for integer values." (3.0.1) >> format(10, '3x') ' a' >>> format(10, '.3x') Traceback (most recent call last): File "<pyshell#2>", line 1, in format(10, '.3x') ValueError: Precision not allowed in integer format specifier (I assume but cannot check that 2.6/7 behave the same.) |
|
|
msg87395 - (view) |
Author: Mark Dickinson (mark.dickinson) *  |
Date: 2009-05-07 19:33 |
This may be a format error rather than a doc error. Eric? |
|
|
msg87396 - (view) |
Author: Mark Dickinson (mark.dickinson) *  |
Date: 2009-05-07 19:34 |
Sorry; ignore me. I should have read more carefully, and paid attention to what was going on on python-dev as well. |
|
|
msg87397 - (view) |
Author: Eric V. Smith (eric.smith) *  |
Date: 2009-05-07 19:35 |
PEP 3101 says it's ignored. I chose to be strict. I don't see the advantage of allowing but ignoring it. |
|
|
msg87398 - (view) |
Author: Eric V. Smith (eric.smith) *  |
Date: 2009-05-07 19:40 |
I updated the docs to say precision is not allowed for integers. |
|
|
msg87423 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2009-05-08 01:37 |
Whoops, I believe my suggested replacement. "A precision is not allowed for integer values." should really be "A precision is not allowed for integer presentation types." or something similar. If you did not change the end of the sentence, please do. A precision *is* allowed for integer values (integers) if a float presentation type is used, because they are auto-converted (though floats are not). This is not documented but I think it should be. See #5965 for this and related suggestions. For future reference, in case anyone ever objects, I agree with remaining strict and changing the doc. 1) Given that the code is strict about the other 'only valid' restrictions (I checked some and it seems to be), it seems reasonable to be consistency strict with precision and ints also. 2) Given that there are 7 presentation types for int and 8 for floats, it is easily possible to make an error. Best to catch it early. The only possible use case I can think of for precision with ints is something like '{0:10.3{1}}'.format(val, typ) # fails for int typs but we fail-proof that, we should also fail-proof '#' with floats: '{0:#10{1}}'.format(val, typ)# fails for float typs |
|
|
msg87430 - (view) |
Author: Eric V. Smith (eric.smith) *  |
Date: 2009-05-08 09:02 |
"integer presentation types" is still not exactly correct, because there are presentation types that work across value types. Specifically, 'n' works on integers and floats. Precision is allowed for floats, but not ints: >>> format(10.0, '.4n') '10' >>> format(10, '.4n') Traceback (most recent call last): File "", line 1, in ValueError: Precision not allowed in integer format specifier Without getting into all sorts of language lawyering, I think it's good enough as-is. |
|
|