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
Usability improvement proposal: Pythonic enums #1251
Conversation
This is great @jorio , we just need to get the names right because we won't be able to change them later. My first reflex would be to do a literal translation from the libgit2 constants, as you have done in some cases, like:
But for others this is not so obvious. For some you have chosen to add the suffix
An alternative would be to namespace the enums and flags, for example:
Instead of:
I think this approach would have the following benefits:
What do you think? |
For reference, if I have not made errors, the proposed name changes in this PR could be summarized like... IntEnum:
IntFlag:
Let's agree on the names first. |
Thank you for your time reviewing this! For context, most class names are actually based on the C typedef name, because the names of the actual values within an enum can be messy. It's difficult to make perfect conversions because there's a lot of overlapping prefixes across different enums. As for your suggestions:
I also believe that users should be free to import enum classes directly ( Here's a proposal for revised names. I attempted to simplify them a bit while still making sure they're distinct from existing classes in pygit2.
Trickier alternative: dispatch enums to relevant classesYet another approach might be to get rid of enums.py and move the enums to the classes they pertain to. In this case, name clashes wouldn't matter anymore. It might clean up the namespaces even more, but there are a couple downsides:
|
Hi @jorio I agree with your points (all in
Only change of mine is that I've changed So the general rule would be:
With few exceptions: If the list of names above looks good, then you can go ahead with the PR. Or tell me if you still want to make changes. |
Hi @jdavid, this looks good to me! I'll amend the PR with the new names soon-ish. Thanks again. |
Hi @jdavid, I rebased the PR on master with the name changes that you suggested. Additional changes since last time:
Let me know if there's anything that still needs tweaking. Thank you! |
Thanks @jorio ! |
This PR introduces "pythonic" IntEnum and IntFlag classes that can supersede
GIT_...
integers in user code.The main benefit of these enum classes is that they naturally guide the user toward the set of flags you can pass to a function, instead of having the user trawl through a list of GIT_ constants. Some examples:
Another benefit is that the new enums are augmented with docstrings. I'm a heavy user of pygit2 and I tend to frequently have to dive into libgit2's source code to recall what some flags do – the new enums reduce this friction:
Notes:
StatusFlags(1<<31)
will still work and it'll convert to a valid int.Repository.state()
(where I just return a raw int if it can't be converted to a RepositoryState).If you think this PR worth merging into pygit2, I'm happy to discuss naming choices, etc.
Next steps: If this PR is accepted, I will submit another one later that makes some C functions return actual Python enums. This way, functions such as
repo.status_file(...)
will return a real StatusFlags instead of an int, which would make it easier to toy with pygit2 in an REPL.