Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

jakirkham
Copy link
Contributor

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 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 ( cython/cython#2732 ).

Any other comments?

NA

@jakirkham jakirkham changed the title Fix conversion of FiniteStatus from C to Python MAINT Fix conversion of FiniteStatus from C to Python Jul 8, 2022
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.
Copy link
Member

@thomasjpfan thomasjpfan left a 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)
Copy link
Member

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.

Copy link
Contributor Author

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

@jakirkham
Copy link
Contributor Author

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 😉

@Micky774 Micky774 added the Needs Decision Requires decision label Jul 16, 2022
Copy link
Member

@jjerphan jjerphan left a 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.

@lorentzenchr
Copy link
Member

@scikit-learn/core-devs Opinions?

I‘m undecided on this one.

@Micky774
Copy link
Contributor

While the solution presented is strictly more correct, I don't think it's that much more correct given the fact that the int vs IntEnum comparison is canonical in Python. That, combined with such a narrow use-case here, leads me to think it is worth being potentially a bit lax (although even then it is still safe) for the sake of keeping the Cython side of things as simple as possible.

I am okay either way, but would probably prefer just to add a comment noting the potential concern w/ the same xref, that way it'll be easy to re-impliment @jakirkham's nice solution if it ever gets promoted to a larger problem before Cython 3

@thomasjpfan
Copy link
Member

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.

@lorentzenchr
Copy link
Member

@thomasjpfan Thanks for the status update on the cython side.
@jakirkham Thanks for opening this PR and triggering the discussion.

In light if the fix in Cython 3.0, I‘ll close.

@jakirkham jakirkham deleted the fix_fs_py_conv branch February 21, 2023 09:08
@jakirkham
Copy link
Contributor Author

All good. As noted above was mostly just flagging this issue after encountering it. Closing seems reasonable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants