pallets / click

Python composable command line interface toolkit
https://click.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
15.71k stars 1.4k forks source link

default value for a `click.File()` option shown as `<_io.TextIOWrapper [...]>` #1999

Closed gaudenz closed 3 years ago

gaudenz commented 3 years ago

The default value of an option with type click.File() is shown as the repr value of the opened file if the file exists. If the default file does not exist, the filename is shown. This is a regression from click version 7.1.2 where the help text always shows the file name.

Create the following minimal program as click-file-open-default:

#!/usr/bin/env python3

import click

@click.command(
    context_settings={
        'show_default': True,
    }
)
@click.option(
    '--file-opt',
    type=click.File(),
    default='my-file',
)
def command(file_opt):
    pass

if __name__ == '__main__':
    command()

Running the program:

# first run while the default file does not exist. This is the expected output
$ ./click-file-option-default --help
Usage: click-file-option-default [OPTIONS]

Options:
  --file-opt FILENAME  [default: my-file]
  --help               Show this message and exit.  [default: False]

# create the file
$ touch my-file 

# run the same command again. The default value in the help output changed.
$ ./click-file-option-default --help
Usage: click-file-option-default [OPTIONS]

Options:
  --file-opt FILENAME  [default: <_io.TextIOWrapper name='my-file' mode='r'
                       encoding='UTF-8'>]
  --help               Show this message and exit.  [default: False]

I would expect the help text to show the filename (literal value of the default option).

Environment:

davidism commented 3 years ago

Fixed with #2085