Skip to content

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

@gaudenz

Description

@gaudenz

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:

  • Python version: 3.9.2
  • Click version: 8.0.1

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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions