Skip to content

Must explicitly convert IntEnum to int to use equality comparison #1848

@baptisteguidoux

Description

@baptisteguidoux

Environment

  • Pythonnet version: 3.0.0-a2
  • Python version: 3.9.10
  • Operating System: Windows 10 Pro - 21H2 - 19044.1766
  • .NET Runtime: Unity/Mono

Details

  • When trying to compare the equality of Python IntEnum instances from C#, they must be explicitly converted to an int.
# in my_module.py file
class MyEnum(enum.IntEnum):
    OK = 1
    ERROR = 2

def get_status():
    return MyEnum.OK 

No crash.

PythonRunner.EnsureInitialized();
using (Py.GIL())
{  
    // Import our module
    dynamic my_module = Py.Import("my_module");
    dynamic MyEnum= my_module .MyEnum; 
    // Call the Python func on C# side
    dynamic status = my_module.get_status();
    if ((int)status == (int)MyEnum.OK) // <-- have to convert to int
    if (status == MyEnum.OK)            // when not converting to int, they are not evaluated as equal (no crash)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy