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

Destructure arguments in function signatures #4231

Open
nickdrozd opened this issue Jun 3, 2024 · 4 comments
Open

Destructure arguments in function signatures #4231

nickdrozd opened this issue Jun 3, 2024 · 4 comments

Comments

@nickdrozd
Copy link

I would like to destructure compound types in function signatures:

type CompoundType = (u64, u64);

#[pyfunction]
pub fn function((arg1, arg2): CompoundType) {}

But this is currently not supported:

error: destructuring in arguments is not supported
  --> src/lib.rs:39:17
   |
39 | pub fn function((arg1, arg2): CompoundType) {}
   |                 ^^^^^^^^^^^^
@davidhewitt
Copy link
Member

While I'm open to this in principle, what would the argument be called on the Python side? At the moment we use the single identifier of the variable as the python argument name.

We could require something like a #[pyo3(name = "foo")] annotation on this pattern argument, I guess. I've wanted the ability to change the Python name of arguments in the past e.g. for unused arguments:

#[pyfunction]
fn bar(#[pyo3(name = "foo")] _foo: Foo) {
   // _foo is never used
}

So maybe with the requirement of needing a Python name annotation, this would be possible. Other opinions welcome!

@alex
Copy link
Contributor

alex commented Jun 3, 2024 via email

@davidhewitt
Copy link
Member

That's a very good point. It's also quite easy to just move the destructuring into the first line of the function, so it's not like this requires a difficult workaround...

@mejrs
Copy link
Member

mejrs commented Jun 8, 2024

I've wanted this to make it easier to work with newtypes but also ran into "then what do we get argument names from" problem. I would prefer an additional line inside the function over something like #[pyo3(name = "foo")]. I'm really not a fan of those.

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

4 participants