Skip to content

Get Value Of An Enum #1585

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

Closed
saroad2 opened this issue Oct 3, 2021 · 6 comments · Fixed by #1661
Closed

Get Value Of An Enum #1585

saroad2 opened this issue Oct 3, 2021 · 6 comments · Fixed by #1661
Milestone

Comments

@saroad2
Copy link

saroad2 commented Oct 3, 2021

Environment

  • Pythonnet version: latest
  • Python version: 3.9.4
  • Operating System: Windows
  • .NET Runtime:

Details

  • Describe what you were trying to get done.

I would like to have the ability to extract the value of a .Net Enum via python. One can do that using MyEnum.value__, however it seems logical that you should do it using int(MyEnum).

import clr

clr.AddReference("System.Windows.Forms")

import System.Windows.Forms as WinForms

enum_value = WinForms.Keys.Control
print(f"{enum_value=}")
print(f"{enum_value.value__=}")

That prints:

enum_value=<System.Windows.Forms.Keys object at 0x000001C053E07580>
enum_value.value__=131072
  • If there was a crash, please include the traceback here.

No crash.

@filmor
Copy link
Member

filmor commented Nov 23, 2021

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?

@lostmsu
Copy link
Member

lostmsu commented Nov 23, 2021

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.

@lostmsu
Copy link
Member

lostmsu commented Nov 23, 2021

As for the issue at hand, .NET enums should implement __int__ or its C API alternative, nb_int for int(Keys.Control) to work, not a codec.

@filmor
Copy link
Member

filmor commented Nov 23, 2021

Isn't that something that we explicitly removed?

@lostmsu
Copy link
Member

lostmsu commented Nov 23, 2021

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.

@saroad2
Copy link
Author

saroad2 commented Nov 29, 2021

So 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.

@lostmsu lostmsu added this to the 3.0.0 milestone Nov 29, 2021
lostmsu added a commit to losttech/pythonnet that referenced this issue Dec 31, 2021
lostmsu added a commit to losttech/pythonnet that referenced this issue Dec 31, 2021
lostmsu added a commit to losttech/pythonnet that referenced this issue Jan 6, 2022
lostmsu added a commit to losttech/pythonnet that referenced this issue Jan 6, 2022
lostmsu added a commit to losttech/pythonnet that referenced this issue Jan 6, 2022
@lostmsu lostmsu linked a pull request Jan 6, 2022 that will close this issue
3 tasks
filmor pushed a commit that referenced this issue Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants