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

Allow more control over argument conversion #3672

Open
adamreichold opened this issue Dec 20, 2023 · 1 comment
Open

Allow more control over argument conversion #3672

adamreichold opened this issue Dec 20, 2023 · 1 comment

Comments

@adamreichold
Copy link
Member

adamreichold commented Dec 20, 2023

We currently extract all arguments using the FromPyObject trait and often run into conflicts whether this should be strict, i.e. basically only downcasting the Python objects to the Python equivalents of the target Rust types, or loose, i.e. using the most general available Python API to produce an instance the target Rust type even if these are conceptually unrelated. Concrete example as are PyBool/bool and whether this should use only downcasting or the full truth value testing procedure or PySequence/Vec which might check whether the Python type is actually or sequence or take any iterable and collect it into a Rust Vec.

One way to alleviate this issue is to allow the user to choose which strategy to pursue, for example by adding a #[pyo3(noconvert)] argument attribute to explicitly request a strict conversion.

@davidhewitt
Copy link
Member

See also #3226 and #2482

An alternative I'm wondering about is that we could default to stricter conversions, which feels more Rust-y, and then have #[pyo3(lax)] to allow conversions.

Implementation-wise, I think we could either add this as a defaulted method to the FromPyObject trait or add a new FromPyObjectLax trait. We'd probably figure out which is nicer quite quickly.

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

2 participants