question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Casting into to cpdef enum

See original GitHub issue

Simplified code:

cpdef enum MyEnum:
    MY_VAL=2

def my_fn():
    return <MyEnum>2

I’d expect my_fn() to return MyEnum.MY_VAL, but just returns the int value.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Jul 8, 2022

Do you have a sense of where it would make sense to add conversion code like this?

(Not Stefan but) I have a 50% written change with the conversion code in as a cdef __Pyx_Enum_{{name}}_to_py(int c_val) in CpdefEnum.pyx (and called from the PyrexTypes.CEnumType.create_to_py_utility_code function).

Agree there is likely a better way to handle accessing the right IntEnum value. Do you have thoughts on what would be the best way to approach this?

I’m not sure there’s a better way to approach this - Cython doesn’t look to have much special knowledge of the Python enum

Also guessing we can do some templating to simplify the branch logic (if that is the right way to go).

yes

0reactions
jakirkhamcommented, Jul 8, 2022

I agree that this behaviour should be changed. Returning the corresponding Enum object is the expected result. In the case of an invalid enum integer value, I’d also expect an exception, probably a ValueError as shown in #2732 (comment). Cython can probably do something more efficient internally than the generic attribute lookup in (<object>MyEnum).ZERO.

Thanks Stefan 🙏

Do you have a sense of where it would make sense to add conversion code like this?

Agree there is likely a better way to handle accessing the right IntEnum value. Do you have thoughts on what would be the best way to approach this?

Also guessing we can do some templating to simplify the branch logic (if that is the right way to go).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to access an enum type by value in cython - Stack Overflow
The problem I see with this technique is that you cannot convert from a string to the original enum class, and in the...
Read more >
Language Basics — Cython 3.0.0a11 documentation
You need to assign a value at some point before first using the variable, ... Declaring an enum as cpdef will create a...
Read more >
Wrapping strongly typed enums - Google Groups
How do I wrap strongly typed enums ("enum class") in Cython? In my file.hpp file I have an Algorithm enum as follows: enum...
Read more >
[Cython] Problem with declaring a map with an enum type.
I have a C enum type that I'm attempting to put in a C++ map: Here's ... from libcpp.map cimport map cdef extern...
Read more >
What is the reason for this Cython compile error when ...
I am working on a Raspberry PI using Python 3.4.2 with Cython 0.24 and GCC 4.9.1. I want to use a cpdef enum...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found