Skip to content

Support set (typing.Set) type for CLI parameters #456

Open
@WillDaSilva

Description

@WillDaSilva

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions