Repository navigation
Get Value Of An Enum #1585
Description
Activity
This is possible using codecs, though they are /really/ unergonomic so far when using them from Python:
import Python.Runtime Python.Runtime.PyObjectConversions.RegisterEncoder(Python.Runtime.Codecs.EnumPyIntCodec.Instance) Python.Runtime.PyObjectConversions.RegisterDecoder(Python.Runtime.Codecs.EnumPyIntCodec.Instance)The decoder is buggy right now (fix in #1621).
@lostmsu Do you have a proposal for a nice ergonomic Python API for this? Should we enable some encoders/decoders by default unless told otherwise?
I don't think enabling codecs by default is a good idea, unless they are 1:1 exact rountrippable mappings.
As for the more ergonomic Python APIs, somebody more proficient with Python should propose those.
As for the issue at hand, .NET enums should implement
__int__or its C API alternative,nb_intforint(Keys.Control)to work, not a codec.Isn't that something that we explicitly removed?
If I remember correctly, we removed the code, that converted any enum value to an integer implicitly when passed to Python. You could never previously get raw enum value in Python, it always just saw integers. So the corresponding type never had
__int__implementation.Reacted by Benedikt ReinartzSo if I understand you correctly, we all agree that adding a default
__int__method for all C# enums is a wise idea?I would have implemented this ability myself, but I have zero knowledge in C#. Let me know if I can help you somehow make this ability happen.
Reacted by Victor- added 5 commits that reference this issue
on Dec 31, 2021 - linked a pull request that will close this issueProvide `__int__` instance method on enum types to support `int(Enum.Member)` #1661
on Jan 6, 2022 - added a commit that references this issue
on Jan 6, 2022
Environment
Details
I would like to have the ability to extract the value of a .Net
Enumvia python. One can do that usingMyEnum.value__, however it seems logical that you should do it usingint(MyEnum).Minimal, Complete, and Verifiable example
this will help us understand the issue.
That prints:
No crash.