Open bindreams opened 2 months ago
I don't see how it's relevant to stub generation and this repo.
I think it's relevant because I'm generating stubs for a pybind11 project and they are incorrect :) I would have used mypy stubgen (which parses enums correctly) but it has multiple other problems.
Do you consider issues related to user-defined type casters out of scope for this project?
What is the generated stub, and what is expected?
Sorry for the back and forth, I should have started with an example.
Here is an example enum in C++
enum class ExampleEnum : int {
A = 0,
B = 1,
C = 1,
D = 2,
};
Here is what pybind11-stubgen
generates, when this enum is bound to python's enum.Enum
or enum.Flag
(or derived classes) respectively:
class ExampleEnum(enum.Enum):
A: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.A: 0>
B: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.B: 1>
D: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.D: 2>
class ExampleEnum(enum.Flag):
B: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.B: 1>
D: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.D: 2>
You can see that C
is missing in both cases because it has a repeated value, and A
is missing in the enum.Flag
case becase it's value is 0. However, the real enum you get in the module obviously contains all of the elements, and they are accessible via the __members__
dict:
>>> dir(ExampleEnum)
['B', 'D', '__class__', ...]
>>> list(iter(ExampleEnum))
[<ExampleEnum.B: 1>, <ExampleEnum.D: 2>]
>>> ExampleEnum.__members__
mappingproxy({'A': <ExampleEnum.A: 0>,
'B': <ExampleEnum.B: 1>,
'C': <ExampleEnum.B: 1>,
'D': <ExampleEnum.D: 2>})
For completeness, the desired stub would look like this:
class ExampleEnum(enum.Enum):
A: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.A: 0>
B: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.B: 1>
C: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.C: 1>
D: typing.ClassVar[ExampleEnum] # value = <ExampleEnum.D: 2>
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:
As you can see, with
iter()
, theDATA
value is missing.