-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
[FEATURE] Literal type support #76
Comments
I think you can solve it with an enum. Here are the docs: https://typer.tiangolo.com/tutorial/parameter-types/enum/ |
This could work indeed |
Great! If that answers your question you can then close the issue. |
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
I don't get why this is closed. Does this mean you won't be adding support for |
This sounds pretty useful to me. |
While things could be rewritten as an enum, it does not play nice with existing typed code which uses typing.Literal to define a set of allowed values. I hope we can revisit this. |
I agree as @f4hy as well. In many cases typing.Literal is a much more elegant solution then using enum.Enum. |
@tiangolo I have a case similar to @f4hy. In my ML project I use smth like I don't wanna rewrite everything else in enum style, it will be tedious and harder to use. |
Can we reopen this ticket? Silly bots closing open tickets because they misinterpret a sentence is quite annoying. |
+1, this would be useful |
Can we have this reopened maybe? Enums are a different usecase. Yes they function the same in the end but they are ergonomically different. Using an enum is good for when the same slection of choices is used somewhere else too, and using a literal is useful when it's a one time thing and it helps show in a very clear way what options are possible in the same line def make_cheese(type: CheeseType) This makes sense for an enum because CheeseType is something that will be useful in more places in the code. def make_food(for_person: AllPersonsICareAbout) This makes less sense, because in my case, I don't care about who I can cook for in other places (for now) def make_food(for_person: Literal['Yarden', 'Bob']): Either way, even if you disagree, the language evidently does agree, meaning those two are different and are both implemented so the argument that they are the same is not passing. You can argue that they are similar enough which makes it none critical to invest resources in implementing this now. I can agree with that, but if that's the case, reopen the issue and have it in the backlog, maybe someone else will implement it |
I could use this as well 🥲 |
Please reopen. |
This issue was closed 4 years ago. Maybe it's time for a new issue or fork? |
Cyclopts supports this feature, see: https://cyclopts.readthedocs.io/en/latest/rules.html#coercion-rules-literal |
Support for the literal type (python 3.8 or via typing_extensions) would be nice. Eg.
The only workarounds (using string or multiple bool flags + validating input) are ugly now.
The text was updated successfully, but these errors were encountered: