Issue 36787: Python3 regresison: String formatting of None object (original) (raw)
Issue36787
Created on 2019-05-03 20:30 by Gawain Bolton, last changed 2022-04-11 14:59 by admin. This issue is now closed.
Messages (4) | ||
---|---|---|
msg341355 - (view) | Author: Gawain Bolton (Gawain Bolton) | Date: 2019-05-03 20:30 |
Python 2.7.16 (default, Apr 6 2019, 01:42:57) [GCC 8.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print('{:^10}'.format(None)) None However this does not work with Python3: Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print('{:^10}'.format(None)) Traceback (most recent call last): File "", line 1, in TypeError: unsupported format string passed to NoneType.__format__ Given that the None type is output as a string, it makes sense for string formatting options to be usable with it. It also makes code less fragile and avoids having to write special cases for when values could be None. | ||
msg341356 - (view) | Author: Gawain Bolton (Gawain Bolton) | Date: 2019-05-03 20:35 |
Note: I have a patch which fixes this. | ||
msg341357 - (view) | Author: Zachary Ware (zach.ware) * ![]() |
Date: 2019-05-03 20:50 |
You can use `!s` to be sure that the object is a string: >>> '{!s:^10}'.format(None) ' None ' I think it's unlikely the behavior of NoneType.__format__ will be changed, but I'm adding Eric Smith to make that determination as the maintainer of str.format. See for the background on the change that produced this behavior. | ||
msg341360 - (view) | Author: Eric V. Smith (eric.smith) * ![]() |
Date: 2019-05-03 22:00 |
This behavior isn't going to change. As Zach says, if you want to convert any value to a string, use !s. We want to fail as soon as possible: if a class (such as NoneType) doesn't implement __format__, then trying to format an instance with a format spec that doesn't apply to it should be an error. If you want to support strings or None, then it's your job to ensure the conversion. As says, one of the big drivers of this change (I'll argue it's really a bugfix) was that you could never add __format__ to class if it didn't previously have one. Or if you did, it would have to support at least the string format spec, since there might be code that formatted it with "^10", for example. As things currently stand, we could add __format__ to NoneType and have "T/F" mean convert to "True" or "False". If NoneTypes were already format-able with "^10", this change wouldn't be possible. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:59:14 | admin | set | github: 80968 |
2019-05-03 22:00:15 | eric.smith | set | status: open -> closedmessages: + assignee: eric.smithresolution: not a bugstage: resolved |
2019-05-03 20:50:24 | zach.ware | set | nosy: + eric.smith, zach.waremessages: + versions: + Python 3.8 |
2019-05-03 20:35:17 | Gawain Bolton | set | messages: + |
2019-05-03 20:30:35 | Gawain Bolton | create |