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

[BUG] Multiple value option gives tuple and not list #127

Closed
jayqi opened this issue Jun 26, 2020 · 8 comments · Fixed by #143
Closed

[BUG] Multiple value option gives tuple and not list #127

jayqi opened this issue Jun 26, 2020 · 8 comments · Fixed by #143
Labels
answered bug Something isn't working

Comments

@jayqi
Copy link

jayqi commented Jun 26, 2020

Describe the bug

The standard way to configure an option that expects multiple values is to declare with List, as described in the docs.

However, this provides a tuple of values, and not a list of values, as one would expect based on the type hint declaration. It also contradicts the documentation, which states "You will receive the values as you declared them, as a list of str".

To Reproduce

Steps to reproduce the behavior with a minimum self-contained file.

  • Create a file main.py with:
from typing import List
import typer


def my_command(opt: List[str] = ["foo", "bar"]):
    typer.echo(opt)
    typer.echo(type(opt))


if __name__ == "__main__":
    typer.run(my_command)
  • Call it with:
python main.py
  • It outputs:
('foo', 'bar')
<class 'tuple'>
  • Call it with:
python main.py --opt baz
  • It outputs:
('baz',)
<class 'tuple'>

Expected behavior

I would expect to get a container of values that is class list as declared.

Environment

  • OS: macOS 10.15.4
  • Python 3.6.10
  • Typer 0.3.0
@jayqi jayqi added the bug Something isn't working label Jun 26, 2020
@ovezovs
Copy link

ovezovs commented Jun 26, 2020

I was able to reproduce this issue. Would this be considered a bug or a documentation issue, @tiangolo? I'd like to work on this.

@ovezovs
Copy link

ovezovs commented Jul 2, 2020

UPDATE: I have discussed this with my MLH mentor and we concluded that it was a bug, however, I am not sure if the bug is in the typing library or Typer itself.

@tiangolo
Copy link
Owner

tiangolo commented Jul 4, 2020

Hey @ovezovs ! Thanks!

Yes, I think it would probably make sense to ensure that the value passed is a list in this case, converting it to a list if it is not.

@hellowhistler
Copy link
Contributor

UPDATE: I have discussed this with my MLH mentor and we concluded that it was a bug, however, I am not sure if the bug is in the typing library or Typer itself.

Hi @ovezovs, are you still planning on looking into this (or already working on it)? I encountered this bug in the wild, and if you've changed your mind, I'd be interested in trying to fix it.

@ovezovs
Copy link

ovezovs commented Jul 16, 2020

Hi @ovezovs, are you still planning on looking into this (or already working on it)? I encountered this bug in the wild, and if you've changed your mind, I'd be interested in trying to fix it.

Feel free to take on it.

@Andrew-Sheridan
Copy link

As an extra layer to this, if an option is declared as foo: Optional[List[str]] = typer.Option(None) it should provide None if nothing is passed. Currently it is providing (), an empty tuple.

In comparison bar: Optional[str] = typer.Option(None) does provide None when missing, as expected.

Would this be considered the same issue, or should I raise a new one?

@tiangolo
Copy link
Owner

tiangolo commented Jul 2, 2022

Thanks for the clear example @jayqi! 🤓 🍰

And thanks for the discussion everyone. ☕

@Andrew-Sheridan, could you please create a new issue with that example?

This was fixed by @hellowhistler in #143

It will be available in the next release, in some hours, Typer version 0.4.2 🎉

@Andrew-Sheridan
Copy link

@Andrew-Sheridan, could you please create a new issue with that example?

Created #410 with the example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants