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: Python3 regresison: String formatting of None object
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Gawain Bolton, eric.smith, zach.ware
Priority: normal Keywords:

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 "<stdin>", line 1, in <module>
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) * (Python committer) 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 issue7994 for the background on the change that produced this behavior.
msg341360 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) 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 issue7994 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:14adminsetgithub: 80968
2019-05-03 22:00:15eric.smithsetstatus: open -> closed
messages: + msg341360

assignee: eric.smith
resolution: not a bug
stage: resolved
2019-05-03 20:50:24zach.waresetnosy: + eric.smith, zach.ware

messages: + msg341357
versions: + Python 3.8
2019-05-03 20:35:17Gawain Boltonsetmessages: + msg341356
2019-05-03 20:30:35Gawain Boltoncreate