pybind / pybind/pybind11-stubgen

When parsing enums derived from `enum.Enum`, non-canonical values are ignored

Open
#223 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
361
Forks
75
PR merge metrics
No merged PRs in 30d

Description

My package does not use pybind11 hacky enums, but instead derives enums from enum.Enum using a popular helper macro.

However, in that case the generated stub does not contain some values, the ones that are considered non-canonical (as defined in this comment). These are zero values (maybe just in enum.Flag) and repeated/alias values.

I haven't looked at the implementation, but the issue is probably related to this: https://github.com/python/cpython/issues/109633. If I'm correct, the issue can probable be fixed by iterating over the __members__ of the enum class.

Example of what I mean:

>>> list(iter(InputMode))
[<InputMode.UNICODE: 1>,
 <InputMode.GS1: 2>,
 <InputMode.ESCAPE: 8>,
 <InputMode.GS1PARENS: 16>,
 <InputMode.GS1NOCHECK: 32>,
 <InputMode.HEIGHTPERROW: 64>,
 <InputMode.FAST: 128>,
 <InputMode.EXTRA_ESCAPE: 256>]

>>> InputMode.__members__
mappingproxy({'DATA': <InputMode.DATA: 0>,
              'UNICODE': <InputMode.UNICODE: 1>,
              'GS1': <InputMode.GS1: 2>,
              'ESCAPE': <InputMode.ESCAPE: 8>,
              'GS1PARENS': <InputMode.GS1PARENS: 16>,
              'GS1NOCHECK': <InputMode.GS1NOCHECK: 32>,
              'HEIGHTPERROW': <InputMode.HEIGHTPERROW: 64>,
              'FAST': <InputMode.FAST: 128>,
              'EXTRA_ESCAPE': <InputMode.EXTRA_ESCAPE: 256>})

As you can see, with iter(), the DATA value is missing.

Contributor guide

No contributing guide indexed for this repository

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 by locating the enum parsing or stub-generation entry point and compare its current iteration with Python enum.Enum.members. Reproduce the example with a zero-valued and repeated enum member, then add a regression test covering non-canonical values; done means the generated stub includes DATA and other members present in members.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.