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.

Title: UserString.join should return UserString
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-03-12 06:32 by Dennis Sweeney, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18953 closed Dennis Sweeney, 2020-03-12 06:46
Messages (5)
msg363995 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2020-03-12 06:32
It seems that `.join` methods typically return the type of the separator on which they are called:

    >>> bytearray(b" ").join([b"a", b"b"])
    bytearray(b'a b')
    >>> b" ".join([bytearray(b"a"), bytearray(b"b")])
    b'a b'

This is broken in UserString.join:

    >>> from collections import UserString as US
    >>> x = US(" ").join(["a", "b"])
    >>> type(x)
    <class 'str'>

Furthermore, this method cannot even accept UserStrings from the iterable:

    >>> US(" ").join([US("a"), US("b")])
    Traceback (most recent call last):
    TypeError: sequence item 0: expected str instance, UserString found.

I can submit a PR to fix this.
msg363996 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-12 06:42
It is a duplicate of issue16397.
msg363997 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2020-03-12 07:01
This is not a duplicate: issue16397 concerned

   " ".join([US("a"), US("b")])

While this is concerned about the return value and acceptable parameters for UserString.join().
msg363998 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-12 07:07
You are right. Sorry.
msg364001 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-03-12 07:52
This API is very old and doesn't seem to have a caused any issues in practice.  Changing the API now is more likely to break existing code than to help anyone in the future.
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84125
2020-03-12 08:04:48Dennis Sweeneysetstatus: open -> closed
resolution: wont fix
stage: patch review -> resolved
2020-03-12 07:52:38rhettingersetmessages: + msg364001
2020-03-12 07:07:49serhiy.storchakasetsuperseder: UserString doesn't combine nicely with strings ->
messages: + msg363998
stage: resolved -> patch review
2020-03-12 07:03:47Dennis Sweeneysetstatus: closed -> open
resolution: duplicate -> (no value)
2020-03-12 07:01:17Dennis Sweeneysetmessages: + msg363997
2020-03-12 06:46:20Dennis Sweeneysetpull_requests: + pull_request18304
2020-03-12 06:42:57serhiy.storchakasetstatus: open -> closed

superseder: UserString doesn't combine nicely with strings

nosy: + serhiy.storchaka
messages: + msg363996
resolution: duplicate
stage: resolved
2020-03-12 06:34:10xtreaksetnosy: + rhettinger
2020-03-12 06:32:03Dennis Sweeneycreate