from typing import BinaryIO
from typing import Optional
from io import StringIO


def decode_object(
    handle: BinaryIO,
    num_lines: Optional[float] = None,
) -> StringIO:
    """ Converts a binary file-like object into a String one.

    Files transferred over http arrive as binary objects.
    If I want to check the first few lines I need to decode it.
    """

    # If we don't specify a number of lines, just use infinity
    if num_lines is None:
        num_lines = float("inf")
    lines = [l.decode() for i, l
             in enumerate(handle)
             if i < num_lines]

    # Create an in memory file like object
    out = StringIO()
    # Write the lines to the file like object.
    out.writelines(lines)

    # Rewind the files to the start so that we can read them
    out.seek(0)
    handle.seek(0)
    return out