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: str.format should support "{:r}" for any type.
Type: Stage: resolved
Components: Interpreter Core, Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.4, Python 3.5
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: al45tair
Priority: normal Keywords:

Created on 2017-09-04 07:01 by al45tair, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg301211 - (view) Author: Alastair Houghton (al45tair) Date: 2017-09-04 07:01
Currently if you do

  "{:r}".format(None)

you get the error message

  TypeError: non-empty format string passed to object.__format__

or on newer versions

  TypeError: non-empty format string passed to NoneType.__format__

(which is at least *some* improvement).

Similar behaviour occurs for other types that don't implement their own __format__ method.

This seems to me to be an error on two counts:

1. object.__format__ *should* support ":r", and probably ":s" too.

2. It's unclear what the error message actually means (I had to Google it to find out what was going wrong), *especially* in a situation where it isn't obvious that the argument evaluates to None.

Since the error itself would still happen if an unsupported specifier was used, we should improve the message.  A better choice might be

  TypeError: format "{:x}" not supported for type NoneType.
msg301212 - (view) Author: Alastair Houghton (al45tair) Date: 2017-09-04 07:35
Having looked carefully through the current code, I notice that the error message *has* been improved (though it'd be nice if it included a copy of the format string it's rejecting), and also that the alternative

  "{!r}".format(None)

does work as expected, and has the advantage that other formatting features are supported, e.g.

  "{!r:10.10}".format(None)

will work as expected.

I'm going to close this bug report.
History
Date User Action Args
2022-04-11 14:58:51adminsetgithub: 75516
2017-09-04 07:35:53al45tairsetstatus: open -> closed

messages: + msg301212
stage: resolved
2017-09-04 07:01:10al45taircreate