typer icon indicating copy to clipboard operation
typer copied to clipboard

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

Open WillDaSilva opened this issue 1 year ago • 3 comments

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

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.

WillDaSilva avatar Sep 19 '22 23:09 WillDaSilva

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

scarf005 avatar Oct 17 '22 06:10 scarf005

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.

WillDaSilva avatar Oct 17 '22 12:10 WillDaSilva

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

WillDaSilva avatar Oct 17 '22 12:10 WillDaSilva