Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flag_value converted to str in Click 8 #1886

Closed
jwodder opened this issue May 12, 2021 · 15 comments
Closed

flag_value converted to str in Click 8 #1886

jwodder opened this issue May 12, 2021 · 15 comments
Labels
f:parameters feature: input parameter types
Milestone

Comments

@jwodder
Copy link
Contributor

jwodder commented May 12, 2021

Consider the following code:

import click

@click.command()
@click.option("--answer", flag_value=42)
def main(answer):
    click.echo(repr(answer))

if __name__ == '__main__':
    main()

Under click 7, passing the --answer option to this script prints 42 (an int), as expected. Under click 8, it prints '42' (a str).

Environment:

  • Python version: 3.9.5
  • Click version: 7.1.2 vs. 8.0.0
@davidism
Copy link
Member

davidism commented May 12, 2021

The default type of parameters is str. You need to specify type=int if you want int values.

@jwodder
Copy link
Contributor Author

jwodder commented May 12, 2021

@davidism OK, but in my actual use case, the flag_value is an instance of a class with a zero-argument constructor. How am I expected to get that to work? Trying @click.option("--answer", is_flag=True, type=Klass) (and also with flag_value=None) results in a Klass() takes no arguments TypeError.

@davidism
Copy link
Member

Then it should use type=click.UNPROCESSED to pass the value through unchanged.

@davidism
Copy link
Member

I think what's affecting this is that we now apply type casting consistently to any value coming through the pipeline. I wonder if setting the type to unprocessed automatically for flag values is safe.

@jwodder
Copy link
Contributor Author

jwodder commented May 12, 2021

@davidism Using type=click.UNPROCESSED works, thanks.

@crashfrog

This comment has been minimized.

@davidism

This comment has been minimized.

@crashfrog

This comment has been minimized.

@davidism

This comment has been minimized.

@crashfrog

This comment has been minimized.

@davidism
Copy link
Member

Which is a callable, not a literal value.

@ThiefMaster
Copy link
Member

A callable is dynamic...

@crashfrog

This comment has been minimized.

@davidism
Copy link
Member

@crashfrog sorry, it sounds like we're having trouble explaining the difference. You're going to have to trust us that there is, in fact, a difference, this tangent is derailing this issue.

@crashfrog

This comment has been minimized.

@davidism davidism added this to the 8.0.1 milestone May 13, 2021
@davidism davidism added the f:parameters feature: input parameter types label May 19, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f:parameters feature: input parameter types
Projects
None yet
Development

No branches or pull requests

4 participants