Skip to content

fix: join() raises TypeError when joining bytes collections - #169

Open
koteshyelamati wants to merge 1 commit into
Suor:masterfrom
koteshyelamati:patch-1
Open

fix: join() raises TypeError when joining bytes collections#169
koteshyelamati wants to merge 1 commit into
Suor:masterfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown

Problem

join([b"foo", b"bar"]) raises TypeError instead of returning b"foobar".

>>> join([b"hello", b" ", b"world"])
TypeError: sequence item 0: expected str instance, bytes found

Root cause

The implementation hardcodes ''.join(colls) — a str join — even when the input is a bytes collection. Python's str.join() cannot accept bytes items.

if isinstance(dest, (bytes, str)):
    return ''.join(colls)  # BUG: always uses str join

Fix

Use cls().join(colls) instead. cls is already computed earlier in the function as dest.__class__, so this naturally dispatches to bytes().join() for bytes and str().join() for strings:

if isinstance(dest, (bytes, str)):
    return cls().join(colls)  # FIX: uses the correct type's join

`join([b"foo", b"bar"])` raises TypeError because the implementation always
uses `''.join(colls)` (a str join) even when joining bytes.

Root cause: the str literal `''` is hardcoded as the separator, which fails
for bytes input since you cannot join bytes with a str separator.

Fix: use `cls().join(colls)` instead — `cls` is already computed from
`dest.__class__`, so this uses `bytes().join()` for bytes and `str().join()`
for strings.

Reproducer:
    join([b"foo", b"bar"])  # TypeError before fix, b"foobar" after
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant