2 ms·
Have constantly struggled over how to deal with this problem. The biggest need for enums has been function arguments which can be one of a few values. In the e
by ramraj07 6y ago
Have constantly struggled over how to deal with this problem. The biggest need for enums has been function arguments which can be one of a few values.
In the end, the Literal class from the typing library seems like the simplest, least wordy solution. Pycharm and mypy capture any mistakes you make which seem more than sufficient for my use cases.
- nerdponx 6y agoWhy not use Enum anyway, which works fine with MyPy? I don't mind the verbosity, it's a one-time commitment to what, 10 extra lines of code?
- ramraj07 6y agoIssue is I'll now need two imports everytime I call this method, which becomes mildly annoying. I was doing this, but a lot of times the Literal type seems more than sufficient. It also seems to play real nice with pydantic so it's an added win!
- nerdponx 6y agoI'm not sure what you mean by "two imports". If you define your Enum subclass in the same file, you use one import for enum.Enu. If you eschew enums and use Literal, you use one import for typing.Literal.