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

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

Open
7 tasks done
WillDaSilva opened this issue Sep 19, 2022 · 3 comments
Open
7 tasks done

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

WillDaSilva opened this issue Sep 19, 2022 · 3 comments
Labels
feature New feature, enhancement or request investigate

Comments

@WillDaSilva
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

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 WillDaSilva added the feature New feature, enhancement or request label Sep 19, 2022
@scarf005
Copy link

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

@WillDaSilva
Copy link
Author

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request investigate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants