-
Notifications
You must be signed in to change notification settings - Fork 794
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
Convert arrays to tuple instead of list #4837
Comments
While your points are very reasonable, I think this is unlikely to be a change that we'd make. Aside from the churn that this would cause for existing code, there's risk of possible inconsistencies if
The above bullets both suggest that For your alternative |
I agree here. I also tend to think about arrays (from a Rust perspective) as a homogeneous collections which puts it in the same bucket as a slice or vector. I think it would very surprising to handle arrays differently from these collections since there are so closely related in Rust. It's true that in Python lists don't have to be homogeneous, but they often are as well. From tuples on the other hand I think it is more likely to expect it may be heterogeneous. I think for the specific example |
@davidhewitt thanks for pointing those cases out. It's very interesting to learn some of the nuances that aren't obvious (to me at least) at surface level, so thanks for explaining.
I'll give this a shot, thanks for the tip! |
@Icxolu Definitely agree, but to clarify, it's not so much the homogenousness that's the motivation for this issue. It's actually the size (number of items). Tuples in Python have a fixed size at runtime and you can tell a type checker what that size is. For example, it makes sense to describe a point in 3D space as a |
Yes, I got that. This was just meant as a different viewing angle on why
Another option to solve this is to just define your own |
With this simple
#[pyclass]
:the Python equivalent becomes
I would argue that since arrays in Rust have a fixed length, it would make more sense to convert this to a
tuple
, such that be Python class becomesI don't know if there are any technical limitations that go against this suggestion, but I think it would make sense to convert fixed-length sequence types to
tuple
.Other thoughts/questions
Is there a way to manually specify how a Rust library type to a pyo3 type? Perhaps, something like this pseudo-code example is possible:
I couldn't find anything in the docs about how to do this in the docs, but if it's there please let me know! It would also help in cases where you dont want a
[u8; N]
to come out asbytes
.You could argue that the type of
inner
should be(u32, u32, u32)
, as according to the conversion table, this will map to a Python tuple. It will cause some longer type annotations of course (in my code base I have 3x3 matrices, typed as[[f32; 3]; 3]
, the alternative would bebut I think this is OK. Don't know if there's a performance penalty to this.
The text was updated successfully, but these errors were encountered: