Instead of Literals, use Enums #737
mikeweltevrede
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
Hi @mikeweltevrede! Thanks for the excellent suggestion. I don't think there is a particular reason why we chose Literals over Enums and I would be open to change =) Maybe @leostimpfle can correct me on this though 😄 @janosg & team recently did a rewrite of optimagic and they went with dataclasses instead of Enums - there's some background on their rationale here: link. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
When I took a look at the repo, I noticed that
pyfixest/estimation/literals.py
usestyping.Literal
for options where I think anEnum
(StrEnum
for Python 3.11+) would make more sense. I would like to hear your thoughts, I am unsure if there was maybe a specific reason Of course, I can work on implementing this.Example
For example, instead of:
you would do:
NB: With
StrEnum
and usingenum.auto
, the right side would benumpylinalgsolve
which would not work, unfortunately.Benefits
Note: generated by ChatGPT and checked by myself.
1. Type Safety and Validation
Literal
, the values are essentially just strings, and while type checking at the static analysis level works, runtime validation is limited.Literal
, you don't have a way to check this dynamically without manually defining the allowed values.2. Readability and Code Organisation
3. Extensibility
4. Namespace and Attribute Access
SolverOptions.NumpyLinAlgSolve
) is less error-prone and more IDE-friendly than string literals.5. Functionality
Beta Was this translation helpful? Give feedback.
All reactions