click icon indicating copy to clipboard operation
click copied to clipboard

Are unimported utilities intentional

Open Rowlando13 opened this issue 3 months ago • 6 comments

This utilities are not imported in init:

  • LazyFile
  • KeepOpenFile
  • make_short_help
  • PacifyFlushWrapper
  • safecall

Is that intentional?

Rowlando13 avatar Sep 30 '25 05:09 Rowlando13

@davidism

Rowlando13 avatar Sep 30 '25 05:09 Rowlando13

Yes, and in the future I'd lean towards not exposing anything new to the top level (or public) without very deliberate consideration.

davidism avatar Sep 30 '25 05:09 davidism

I will also add private to the doc strings since they have no leading _.

Rowlando13 avatar Oct 01 '25 03:10 Rowlando13

Maybe we can even move them to the _utils.py file.

kdeldycke avatar Oct 08 '25 09:10 kdeldycke

I think we should rename them with leading _ and move into _utils if there is not a better spot for them.

Rowlando13 avatar Nov 22 '25 19:11 Rowlando13

Another proposal: if we have a lot of file-related private utilities, we can move them all to an _io.py file. Which can also anticipate a future public facing io.py file.

kdeldycke avatar Nov 23 '25 03:11 kdeldycke

Apologies for butting in, but if LazyFile is being made private, is there a more appropriate type hint for parameters receiving lazily-opened files? In my own CLIs, I'm currently using it in that fashion, e.g.:

from typing import TYPE_CHECKING

import click

from . import cook_the_data


if TYPE_CHECKING:
    from io import BufferedReader

    from click.utils import LazyFile


@click.command()
@click.argument("in_file", type=click.File("r"))
@click.argument("out_file", type=click.File("w"))  # Implicitly lazy.
def do_stuff(*, in_file: BufferedReader, out_file: LazyFile) -> None:
    raw_data = in_file.read()
    cooked_data = cook_the_data(raw_data)

    with out_file.open() as out_file_writer:
        out_file_writer.write(cooked_data)

It would suck to lose the ability to correctly type-hint out_file, here. 🙂

benblank avatar Dec 15 '25 21:12 benblank

It should not matter if the file is lazy or not, it has the same interface as a regular file.

davidism avatar Dec 15 '25 23:12 davidism

Okay, I see where my misunderstanding was; I had missed LazyFile.__getattr__ and thought lazy files needed to be explicitly .open()ed before use. Thanks for helping clear that up!

benblank avatar Dec 16 '25 00:12 benblank