Skip to content
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

Open
MartinJepsen opened this issue Jan 5, 2025 · 0 comments
Open

Convert arrays to tuple instead of list #4837

MartinJepsen opened this issue Jan 5, 2025 · 0 comments

Comments

@MartinJepsen
Copy link

MartinJepsen commented Jan 5, 2025

With this simple #[pyclass]:

#[pyclass(get_all)]
struct Example {
    inner: [u32; 3]
}

the Python equivalent becomes

class Example:
    inner: list[int]

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 becomes

class Example:
    inner: tuple[int, int, int]

I 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:

    #[pyclass(get_all)]
    struct Example {
        #[pyo3(type="tuple[int, int, int]")]   // Maybe like this
        #[pyo3(type=pyo3::types::PyTuple)]     // or like this
        inner: [u32; 3]

    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 as bytes.

  • 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 be

    (
        (f32, f32, f32),
        (f32, f32, f32),
        (f32, f32, f32),
    )

    but I think this is OK. Don't know if there's a performance penalty to this.

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

No branches or pull requests

1 participant