-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
MAINT Fix conversion of FiniteStatus
from C to Python
#23858
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
Conversation
FiniteStatus
from C to PythonFiniteStatus
from C to Python
Currently when Cython converts a `cpdef enum` from C to Python, it constructs a Python `int` instead of using the `IntEnum`-based `FiniteStatus` Python object created by `cpdef`. As `IntEnum` values can be compared with `int`, this doesn't cause issues atm. That said, to avoid issues it would be good to return `FiniteStatus` objects to Python code. This adds some code to manually perform this conversion between C and Python code to workaround this Cython issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
As IntEnum values can be compared with int, this doesn't cause issues atm.
IntEnum
values being compared to ints is documented behavior. I suspect the behavior will not change in the future.
Overall, I am -1 with this change because of the added complexity.
@@ -14,7 +14,21 @@ def cy_isfinite(floating[::1] a, bint allow_nan=False): | |||
cdef FiniteStatus result | |||
with nogil: | |||
result = _isfinite(a, allow_nan) | |||
return result | |||
return _convert_FiniteStatus_c_to_py(result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is additional complexity from mapping the C enum values to Python ones. I can see the reasoning behind doing this conversion, but I am content with the current behavior of returning a int.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah ideally this issue would be solved in Cython. There are templating mechanisms available there, which would alleviate the complexity inherent in manually mapping these. Not to mention saving libraries from writing it
Thanks for taking a look, Thomas 🙏 No strong feelings on whether this, some other change, or no change goes in 🙂 Mostly wanted to make others aware of this issue that I stumbled into playing with this code 😉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. To me, this is a reasonable fix. I think we should not use enumerations before cython/cython#2732 is fixed.
@scikit-learn/core-devs Opinions? I‘m undecided on this one. |
While the solution presented is strictly more correct, I don't think it's that much more correct given the fact that the I am okay either way, but would probably prefer just to add a comment noting the potential concern w/ the same |
With cython/cython#4877 merged, the Cython issue was fixed and an Python enum is returned with the development build of Cython. Since the correct behavior will be in Cython 3.0, I am -1 on this PR. |
@thomasjpfan Thanks for the status update on the cython side. In light if the fix in Cython 3.0, I‘ll close. |
All good. As noted above was mostly just flagging this issue after encountering it. Closing seems reasonable |
Reference Issues/PRs
Follow up to PR ( #23849 ) and PR ( #23197 ).
What does this implement/fix? Explain your changes.
Currently when Cython converts a
cpdef enum
from C to Python, it constructs a Pythonint
instead of using theIntEnum
-basedFiniteStatus
Python object created bycpdef
. AsIntEnum
values can be compared withint
, this doesn't causeissues atm. That said, to avoid issues it would be good to return
FiniteStatus
objects to Python code. This adds some code to manually perform this conversion between C and Python code to workaround this Cython issue ( cython/cython#2732 ).Any other comments?
NA