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

How do I pass the None value explicitly? #530

Open
7 tasks done
salmiakki opened this issue Jan 10, 2023 · 2 comments
Open
7 tasks done

How do I pass the None value explicitly? #530

salmiakki opened this issue Jan 10, 2023 · 2 comments
Labels
question Question or problem

Comments

@salmiakki
Copy link

salmiakki commented Jan 10, 2023

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

from typing import Optional

import typer


def main(
    limit: Optional[int] = typer.Option(11),
):
    print(limit)

if __name__ == "__main__":
    typer.run(main)

Description

How do I pass the None value for an Optional argument that has a non-None default value?

Operating System

macOS

Operating System Details

No response

Typer Version

0.7.0

Python Version

Python 3.9.13

Additional Context

No response

@salmiakki salmiakki added the question Question or problem label Jan 10, 2023
@jonaslb
Copy link

jonaslb commented Feb 6, 2023

Semantically None is used to communicate the absence of a specified value in typer. So you can't as far as I can tell set it explicitly or enable functionality to allow it.

My proposal is that you either use a special value (for example -1 or 0) to indicate the limit, or to create a special --disable-limit flag, like so:

from typing import Optional

import typer

def main1(
    limit: Optional[int] = typer.Option(11, help="A limit value of 0 disables the limit.")
):
    if limit > 0:
        print(limit)
    else:
        print("unlimited")

def main2(
    limit: Optional[int] = 11,
    disable_limit: bool = typer.Option(False, "--disable-limit")
):
    if disable_limit:
        limit = None
    print(limit)

if __name__ == "__main__":
    typer.run(main1)

jawnsy added a commit to PrefectHQ/prefect-helm that referenced this issue Jul 3, 2023
The default value for --limit configured in our typer.Option is
None; however, users cannot explicitly set the value to None:
tiangolo/typer#530

Closes: #192
jawnsy added a commit to PrefectHQ/prefect-helm that referenced this issue Jul 3, 2023
The default value for --limit configured in our typer.Option is
None; however, users cannot explicitly set the value to None:
tiangolo/typer#530

Closes: #192
@kuykendall-ben
Copy link

Update here: with custom parsers, you should be able to implement this as of typer 0.8.0!

from typing import Optional
import typer

def int_or_none(raw: str) -> Optional[int]:
    return None if raw == 'None' else int(raw)

def main(
    limit: Optional[int] = typer.Option(11, parser=int_or_none),
):
    print(limit)

if __name__ == "__main__":
    typer.run(main)

Usage:

$ /usr/bin/python3 main.py       
11
$ /usr/bin/python3 main.py --limit None
None
$ /usr/bin/python3 main.py --limit 3   
3

The name of the parser is even displayed in the help string:

$ /usr/bin/python3 main.py --help      
Usage: main.py [OPTIONS]

Options:
  --limit INT_OR_NONE  [default: 11]
  --help               Show this message and exit.

You could modify the metavar or help field of your Option for additional clarify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

3 participants