-
Notifications
You must be signed in to change notification settings - Fork 53
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
When parsing enums derived from enum.Enum
, non-canonical values are ignored
#223
Comments
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 :) 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,
}; Code to generate a pybind11 binding using the helper macro I mentioned above// Global scope
P11X_DECLARE_ENUM(
"ExampleEnum",
"enum.Enum", // or "enum.Flag", etc.
{"A", ExampleEnum::A},
{"B", ExampleEnum::B},
{"C", ExampleEnum::C},
{"D", ExampleEnum::D}
)
PYBIND11_MODULE(module_name, m) {
p11x::bind_enums(m);
} Here is what 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 >>> 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: python/cpython#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.The text was updated successfully, but these errors were encountered: