This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Doc error: integer precision in formats
Type: Stage:
Components: Documentation Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, georg.brandl, mark.dickinson, terry.reedy
Priority: normal Keywords:

Created on 2009-05-07 19:31 by terry.reedy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg87394 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) 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 <module>
    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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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 "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier

Without getting into all sorts of language lawyering, I think it's good
enough as-is.
History
Date User Action Args
2022-04-11 14:56:48adminsetgithub: 50213
2009-05-08 09:02:25eric.smithsetmessages: + msg87430
2009-05-08 01:37:38terry.reedysetmessages: + msg87423
2009-05-07 19:40:19eric.smithsetstatus: open -> closed
resolution: accepted
messages: + msg87398
2009-05-07 19:35:45eric.smithsetassignee: georg.brandl -> eric.smith
2009-05-07 19:35:10eric.smithsetmessages: + msg87397
2009-05-07 19:34:55mark.dickinsonsetmessages: + msg87396
2009-05-07 19:33:02mark.dickinsonsetnosy: + mark.dickinson, eric.smith
messages: + msg87395
2009-05-07 19:31:49terry.reedycreate