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

Empty tuple returned for Option instead of expected None when no values passed #410

Closed
7 tasks done
Andrew-Sheridan opened this issue Jul 3, 2022 · 2 comments
Closed
7 tasks done
Labels
bug Something isn't working

Comments

@Andrew-Sheridan
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • 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

#127 (comment)

@Andrew-Sheridan Andrew-Sheridan added the question Question or problem label Jul 3, 2022
@Andrew-Sheridan Andrew-Sheridan changed the title Incorrect type returned for Option defaults Empty tuple returned for Option instead of expected None when no values passed Jul 3, 2022
@jonasjancarik
Copy link

jonasjancarik commented Aug 30, 2022

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/)

@svlandeg
Copy link
Collaborator

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 #170, and close this one to keep the discussion in one thread.

@svlandeg svlandeg added bug Something isn't working and removed question Question or problem labels Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants