[issue34425] :s formatting broken for objects without __format__

2018-08-17 Thread Jason Spencer


New submission from Jason Spencer :

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 "", line 1, in 
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 
<https://bugs.python.org/issue34425>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue34425] :s formatting broken for objects without __format__

2018-08-20 Thread Jason Spencer


Jason Spencer  added the comment:

Then I would argue that it is at least a documentation bug.  The 3.6 format
spec mini language still claims that {} is equivalent to {:s}, which now is
only conditionally true.

I would also argue that having different behavior for {} and {:s}, which
are semantically the same in the client's eye, forces the client code to
understand more about the object they are stringifying than should be
necessary.  If one works, so should the other by all descriptions of the
format spec.  If the format spec is *just* ':s', the library should call
the target's __str__ if __format__ is not defined.  The library seems
willing to do this for the empty format string, but not when the client
code specifically asks for a string

On Sat, Aug 18, 2018 at 10:12 AM Eric V. Smith 
wrote:

>
> Eric V. Smith  added the comment:
>
> I agree this is the desired behavior, and not a bug.
>
> Because you are not specifying a __format__ in your class,
> object.__format__ is being called. By design, it does not understand any
> formatting specifiers, instead reserving them for your class to implement.
> "!s" is the correct way to convert your type to a string. Either that, or
> add a __format__ that understands "s".
>
> Note that not all types understand "s", for example, datetime.
>
> --
> assignee:  -> eric.smith
> nosy: +eric.smith
> stage:  -> resolved
> status: pending -> closed
>
> ___
> Python tracker 
> <https://bugs.python.org/issue34425>
> ___
>

--

___
Python tracker 
<https://bugs.python.org/issue34425>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com