click icon indicating copy to clipboard operation
click copied to clipboard

Using a class as a `flag_value` causes the default to be instantiated in click 8.3.0

Open jwodder opened this issue 2 months ago • 1 comments

Consider the following code:

import click

class Foo:
    pass

class Bar:
    pass

@click.command()
@click.option("--foo", "ty", flag_value=Foo, type=click.UNPROCESSED, default=True)
@click.option("--bar", "ty", flag_value=Bar, type=click.UNPROCESSED)
def main(ty):
    click.echo(repr(ty))

if __name__ == "__main__":
    main()

When run with no arguments using click 8.3.0, this prints <__main__.Foo object at 0x10bed92b0>, indicating that Foo has been instantiated, which is not what I want. When using an older version of click, the script outputs <class '__main__.Foo'>, which is what I want.

If the script is passed an explicit --foo or --bar option, a class is output, indicating that the problem lies with the default handling.

Changing the default=True to default=Foo does not help.

jwodder avatar Oct 22 '25 14:10 jwodder

This isn't exclusive to classes obviously -- I'm finding this bug because behavior has changed in 8.3 for regular callables as well (e.g. a project of mine which uses flag_value callables is broken under 8.3).

Julian avatar Oct 26 '25 01:10 Julian