Skip to content

stdin click.File has .name attribute when running from CLI, but doesn't when using CliRunner #1064

@nils-werner

Description

@nils-werner

Expected Result

@click.argument('infile', type=click.File('r')) has attribute name in all invokation scenarios

Actual result

@click.argument('infile', type=click.File('r')) doesn't have attribute name when using click.testing.CliRunner.invoke()

Steps to reproduce

@click.command()
@click.argument('infile', type=click.File('r'))
def test(infile):
    print(infile)
    print(infile.name)
test()

run as

python test.py -

returns

<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
<stdin>

whereas

@click.command()
@click.argument('infile', type=click.File('r'))
def test(infile):
    print(infile)
    print(infile.name)  # Raises AttributeError

from click.testing import CliRunner

runner = CliRunner()
result = runner.invoke(test, ['-'], input="")
print(result.output)
print(result)

returns

<_io.TextIOWrapper encoding='utf-8'>

<Result AttributeError("'_io.BytesIO' object has no attribute 'name'",)>

Metadata

Metadata

Assignees

No one assigned

    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