-
Notifications
You must be signed in to change notification settings - Fork 760
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
PyClass Sequence of tuples or arrays not convertible to numpy array #2392
Comments
As @georgios-ts pointed out in Qiskit/rustworkx#614 (comment) the underlying issue might be that the new |
Ugh, that's frustrating. The problem I saw with TBH while annoying I've been coming round to the opinion that maybe it's the lesser evil compared to not setting What would your opinion be? Is CPython adding the length to negative indices a problem in practice? |
I'm not the original reporter, but I've had a problem with my code not handling negative indexes correctly after migration from |
The "not handling correctly" just means that we aren't handling negative indices specially anymore, I think. This is intended. |
I agree with @birkenfeld in that I felt having special handling for negative integers to be a footgun. For example, imagine a sequence with length 5, and user attempts to index with -8. If Further, the To be honest, both having and not having My preferred solution would be to add a new type object flag to CPython to opt-out of this negative index adjustment so that implementations can handle it. That way PyO3 could return to implementing |
Well, this sounds indeed like a problem in CPython but from my perspective pyo3 should not try to fight that (and it didn't a few releases back). It's best to fill in |
While I'm leaning towards this indeed, we need to be careful because anyone who is converting indices manually like @ravenexp will end up with broken code if we start emitting |
I was thinking about this a bit more, maybe in the interest of compatibility we can add an explicit sequence flag to |
I think that's a reasonable plan. If anyone's got availability to write a PR for this, would be much appreciated. Otherwise I'll get to it eventually.... (Maybe in a few weeks though) |
Sequence users - we're thinking to change I was wondering if you have any opinions on this? |
Implementation for #2392 (comment) now at #2567. @mtreinish are you willing to double-check that branch works for you before I merge it? |
#2567 now merged, can test on main if desired. Release incoming soon, I will close this. |
I can't get Am I missing something? |
I think #2392 (comment) and your experience illustrates exactly why this CPython behaviour is very confusing. I will put a longer answer on #2601. |
Bug Description
When creating a custom pyclass that implements the sequence protocol with pyo3 0.16.x the output type is missing something (I haven't been able to figure out exactly what) that prevents numpy from interpreting it correctly to convert into an array. This only is an issue when using the creating a sequence type using
#[pymethods]
. It works as expected if you use the deprecated#[pyproto]
with thePySequenceProtocol
trait.Steps to Reproduce
MySequenceArray
orMySequenceTuple
:Backtrace
Your operating system and version
Linux
Your Python version (
python --version
)3.10.0
Your Rust version (
rustc --version
)1.60.0
Your PyO3 version
0.16.x
How did you install python? Did you use a virtualenv?
pacman
Additional Info
This example worked as expected with the deprecated
PySequenceProtocol
trait andpyproto
. So the pyclass code becomes:then running:
outputs the expected:
The text was updated successfully, but these errors were encountered: