[issue39944] UserString.join should return UserString

Dennis Sweeney report at bugs.python.org
Thu Mar 12 02:32:03 EDT 2020


New submission from Dennis Sweeney <sweeney.dennis650 at gmail.com>:

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.

----------
components: Library (Lib)
messages: 363995
nosy: Dennis Sweeney
priority: normal
severity: normal
status: open
title: UserString.join should return UserString
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39944>
_______________________________________


More information about the Python-bugs-list mailing list