typer
typer copied to clipboard
Empty tuple returned for Option instead of expected `None` when no values passed
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the Typer documentation, with the integrated search.
- [X] I already searched in Google "How to X in Typer" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to Typer but to Click.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
import typer
from typing import Optional, List
app = typer.Typer(add_completion=False)
@app.command()
def main(
foos: Optional[tuple[str]] = typer.Option(None, "--foo"),
bar: Optional[str] = typer.Option(None, "--bar"),
):
print(f"{type(foos)=}, {foos=}")
print(f"{type(bar)=}, {bar=}")
if __name__ == "__main__":
app()
Description
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.
❯ python issue.py --help
Usage: issue.py [OPTIONS]
Options:
--foo TEXT
--bar TEXT
--help Show this message and exit.
Nothing passed causes the returned value for foo
to be an empty tuple, it should be None
:
❯ python issue.py
type(foos)=<class 'tuple'>, foos=()
type(bar)=<class 'NoneType'>, bar=None
Passing values it returns a tuple of the values
❯ python issue.py --foo Hello --foo World --bar Goodbye
type(foos)=<class 'tuple'>, foos=('Hello', 'World')
type(bar)=<class 'str'>, bar='Goodbye'
Operating System
macOS
Operating System Details
No response
Typer Version
0.4.1
Python Version
3.10.0
Additional Context
https://github.com/tiangolo/typer/issues/127#issuecomment-721710767
The same thing happens with lists (an empty list is returned instead of None
) when you use Optional[List[str]] = typer.Option(None)
(as suggested in https://typer.tiangolo.com/tutorial/multiple-values/multiple-options/)
Hi, thanks for the report! It looks like this is still an issue. I'll go ahead and merge this issue report with the similar report in https://github.com/tiangolo/typer/issues/170, and close this one to keep the discussion in one thread.