tiangolo / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.

Home Page:https://typer.tiangolo.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support set (`typing.Set`) type for CLI parameters

WillDaSilva opened this issue · comments

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 List, Set

import typer

app = typer.Typer(name="set_example")


@app.command()
def list_of_str(list_of_str: List[str]):
    typer.echo(f"list_of_str: {list_of_str!r}")


# @app.command()
# def set_of_str(set_of_str: Set[str]):
#     typer.echo(f"set_of_str: {set_of_str!r}")


if __name__ == "__main__":
    app()

Description

The code above should work with the commented section uncommented. Currently, if it is uncommented and run, it will raise:

RuntimeError: Type not yet supported: typing.Set[str]

Wanted Solution

Similar to how Typer supports using typing.List to accept a CLI parameter as a list of some supported atomic type (e.g. str, int, float, etc.), it should also support using typing.Set. It would be as if the parameter used typing.List, but then before returning the list to the command function, set is called on it. This provides some advantages, such as:

  • removing duplicates
  • removing argument order information
  • providing a data structure with fast inclusion checking

Wanted Code

from typing import List, Set

import typer

app = typer.Typer(name="set_example")


@app.command()
def list_of_str(list_of_str: List[str]):
    typer.echo(f"list_of_str: {list_of_str!r}")


@app.command()
def set_of_str(set_of_str: Set[str]):
    typer.echo(f"set_of_str: {set_of_str!r}")


if __name__ == "__main__":
    app()

Alternatives

No response

Operating System

Linux, Windows, macOS

Operating System Details

No response

Typer Version

0.6.1

Python Version

3.7.9

Additional Context

I would like to implement this feature myself.

commented

hi, would it be okay if I made a PR for it?

Fine by me @scarf005, but I don't know if the PR will be accepted by @tiangolo.

I've got some WIP here: https://github.com/WillDaSilva/typer/tree/set-params. Nothing much, but it might help.

Oh, nevermind about the WIP @scarf005. I see you've already opened a PR for this. Thanks!