PyO3 / PyO3/pyo3

More gooder and easier enums

Open
#5,389 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Hi! I just learned PyO3, and dumped a handful of structural bio libs onto PyPi. Here's the good: Pretty much everything.

Here is the bad: Enums. I'm a big enum guy. I think many or most rust users are. I got ChatGPT to get my enums working in one of the libs, but it's a mess of macros and repeated enum fields. Even wrapped ones. Cool!

However... I'm not sure how I can maintain that. Let's shelve wrapped enums (ADTs or w/e), and talk pure enums. I can write this in Rust:

enum Things {
    A,
    B,
}

I can write this equiv python:

class Things(Enum):
    A = auto(),
    B = auto(),

Do you think we could make an easy mode for enums that just works, and does essentially that translation, or something like it? We can keep using the LLM-macro-madness for wrapped enums, as I don't have a good answer for how that would work. (BC there is no 1:1 mapping in Python for rust's wrapped enums).

Note: In some cases, you currently can also do a hack where the getter just returns the enum's Display or Debug enum; can work in some cases.

Note: I gather classes in Python are kind of fuzzy in their implementation. So, it doesn't have to be an enum.Enum, as long as it acts like one.

Also note: Searching the internet, it seems there is currently no good solution, hence my post here. If y'all think we can get something official, great. If not, I suspect I can do this with macros, maybe. Or a wizard can. I'm not trying to use strings (As some suggest, and I suggested as a hack); I want actual enum-based APIs, which I do in Python, even though it's not a common pattern.

Ref: Here's the workaround I mentioned. I don't mind having macros (Although ideally that's in its own lib or built in to PyO3), but don't like the repetition. That lib is a good example, because its main purpose is to provide enums for Amino Acids, Elements, and Nucleotides, as a backbone for other projects.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the workaround in python/src/lib.rs and compare its repeated enum and macro structure with the pure-enum behavior requested for PyO3. Define what an official easy mode must expose for Rust unit variants, and consider how completion would be verified without extending the request to wrapped enums.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.