Title: collections.UserString format and format_map return a string
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: bizywizy, eric.smith, rhettinger
Priority: normal Keywords: patch

Created on 2020-01-16 09:18 by bizywizy, last changed 2020-01-17 04:32 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18026 closed bizywizy, 2020-01-16 09:25
Messages (2)
msg360101 - (view) Author: bizywizy (bizywizy) * Date: 2020-01-16 09:18
collections.UserString.format and collections.UserString.format_map return a string instaed of UserString.

This is quite weird because I expect that the %-syntax and `format` method have to produce the same result.

>>> isinstance(UserString('Hello %s') % 'World', UserString)
>>> isinstance(UserString('Hello {}').format('World'), UserString)
msg360159 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-01-17 04:32
It is too late is the life of UserString for a design change, so I'm going to have to close this as more likely to harm existing code than to benefit future code.

Also, despite you expectations, this isn't atypical for Python classes.  For example, the __add__ method on integer subclasses returns actual ints rather than instances of the subclass.

In the case of UserString, the current design can be useful because it make UserString instances more substitutable for regular strings.
Date User Action Args
2020-01-17 04:32:40rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg360159

resolution: rejected
stage: patch review -> resolved
2020-01-16 10:05:06eric.smithsetnosy: + eric.smith
2020-01-16 09:25:59bizywizysetkeywords: + patch
stage: patch review
pull_requests: + pull_request17421
2020-01-16 09:18:53bizywizycreate