Skip to content

Echoing empty bytes or bytearray raises TypeError #3487

@Iain-S

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"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions