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.

Title: Format Specs: doc 's' and implicit conversions
Type: Stage: resolved
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: brian.curtin, eric.smith, georg.brandl, lrekucki, terry.reedy
Priority: normal Keywords: needs review, patch

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

File name Uploaded Description Edit
issue5965-1.diff eric.smith, 2010-02-24 18:11
Messages (10)
msg87421 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-05-08 01:31
String Services / Format Specification Mini-Language ( in 3.1)
Building on #5963: document type 's' and implicit conversions.

Near the top, after
"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 that result."

[This applies to all formats but particularly explains the point of 's',
present or implied.]

In the grammar box, add '" s " |' to the front of the list of types.

After "Finally, the type determines how the data should be presented." add
"Non-number values require 's' or no type.  Numbers are not allowed with

At the very end, add

"If an integer is used with a non-integer number presentation type
(those above other than 'n' or none) it is converted with float(). 
However, using a float or decimal with an integer-only presentation type
is not allowed."
msg87422 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2009-05-08 01:35
I forgot to include:

Error messages for mismatches between specification type and value type
currently look like

"ValueError: Unknown conversion type d"

They would be *much* clearer written as something like

"ValueError: Conversion type d is invalid for float values"

and similarly for similar errors.
msg87445 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-05-08 15:22
I'll look at it. The comma stuff needs to be added for 2.7 and 3.1, too.
msg97286 - (view) Author: Łukasz Rekucki (lrekucki) Date: 2010-01-06 01:21
I see a problem with silently converting types without __format__() using str(). The user may write `"{:10s}".format(some_obj)`, which will work as long as some_obj doesn't have a formatter of it's own. If I later on, decide I want to add some custom formating to that object, there is a large chance I'll break a lot of code, 'cause doing it in backward-compatible way seems very difficult.
msg100026 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-24 11:02
Adding __format__ breaking existing calls to object.__format__ is issue 7994.
msg100029 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-24 11:17
I think the only remaining issue here is that 's' isn't documented. The exceptions are now more meaningful, and commas have been documented.
msg100032 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-24 13:01
Here's my proposed changes. Please review.

I put the string type 's' into its own table. That's probably overkill, but I think it gets lost if it's just mentioned in the text. Feel free to change that, though.

Also, I'm not wild about my wording for the int->float conversions. But I think it needs to go near the int presentation types, not at the end of the section. I'd actually prefer it at the top of the int table, but I couldn't find a way of working it in.
msg100039 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2010-02-24 15:49
As for the int->float paragraph, how does this sound for the last sentence? "When doing so, the :func:`float` function is used before formatting to convert the integer value to a floating point number."
msg100061 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-24 18:11
That looks good. I tweaked it a little and fixed a few other problems with the patch. New patch attached.
msg100086 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-02-25 14:20
Checked in.

trunk: r78440
release26-maint: r78441
py3k: r78442
release31-maint: r78443
Date User Action Args
2022-04-11 14:56:48adminsetgithub: 50215
2010-02-25 14:20:59eric.smithsetstatus: open -> closed
resolution: accepted
messages: + msg100086

stage: patch review -> resolved
2010-02-24 18:11:32eric.smithsetfiles: - issue5965.diff
2010-02-24 18:11:13eric.smithsetfiles: + issue5965-1.diff

messages: + msg100061
2010-02-24 15:49:58brian.curtinsetnosy: + brian.curtin
messages: + msg100039
2010-02-24 13:02:01eric.smithsetfiles: + issue5965.diff
priority: normal
messages: + msg100032

keywords: + patch, needs review
stage: patch review
2010-02-24 11:17:50eric.smithsetmessages: + msg100029
versions: + Python 3.2, - Python 3.0
2010-02-24 11:02:51eric.smithsetmessages: + msg100026
2010-01-06 01:21:29lrekuckisetnosy: + lrekucki
messages: + msg97286
2009-05-08 15:22:28eric.smithsetmessages: + msg87445
2009-05-08 12:18:14georg.brandlsetassignee: georg.brandl -> eric.smith

nosy: + eric.smith
2009-05-08 01:35:32terry.reedysetmessages: + msg87422
2009-05-08 01:31:30terry.reedycreate