Skip to content

Echoing empty bytes or bytearray raises TypeError #3487

Closed

Description

@Iain-S

Hopefully this isn't too frivolous but I think it's an easy fix: echoing an empty byte string will cause a crash if nl=True.

To replicate:

$ python -c "import io; import click; click.echo(b'', io.BytesIO(), nl=True)"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/myproject/click/src/click/utils.py", line 337, in echo
    file.write(out)  # type: ignore
    ^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'

Expected behaviour:

We expect b"\n" to be written to the stream and for TypeError not to be thrown.

Environment:

  • Python version: 3.11.2
  • Click version: 8.5.0.dev0

Suggested fix:

Change

out = out or ""

    if nl:
-      out = out or ""
+      out = "" if out is None else out 
       if isinstance(out, str):
            out += "\n"
        else:
            out += b"\n"

Activity

  1. davidism commented on May 20, 2026

    @davidism
    Member

    Makes sense, surprised this never had a test. Happy to review a human-written PR.

  2. deleted a comment from macayu17 on May 20, 2026
  3. added this to the 8.4.1 milestone on May 21, 2026
  4. locked as resolved and limited conversation to collaborators on Jul 24, 2026
  5. added
    help output
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help output

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions