[New-bugs-announce] [issue34425] :s formatting broken for objects without __format__
Jason Spencer
report at bugs.python.org
Fri Aug 17 20:18:56 EDT 2018
New submission from Jason Spencer <jasonspencer at google.com>:
Objects with __str__ but WITHOUT __format__ are string-convertible and default-to-str formattable. But the explicit use of '{:s}' as a format string fails to format these objects as expected. Either it is no longer the case that '{}' and '{:s}' are equivalent format strings, or formatting for {:s} is broken. Users would not expect the following to be true.
(Tested in 3.5.3 and 3.6. Maybe be the same in later releases.)
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Coord:
... def __init__(self, a, b):
... self.a=a
... self.b=b
... def __str__(self):
... return '{:d}:{:d}'.format(self.a, self.b)
...
>>> c = Coord(3,4)
>>> str(c)
'3:4'
>>> '{:s}'.format(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to Coord.__format__
>>> '{}'.format(c)
'3:4'
>>> '{!s:s}'.format(c)
'3:4'
>>>
----------
components: Interpreter Core
messages: 323688
nosy: Jason Spencer
priority: normal
severity: normal
status: open
title: :s formatting broken for objects without __format__
type: behavior
versions: Python 3.5, Python 3.6
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34425>
_______________________________________
More information about the New-bugs-announce
mailing list