pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Review conversions and ensure consistency

Open davidhewitt opened this issue 2 years ago • 4 comments

Continued from https://github.com/PyO3/pyo3/pull/3197#discussion_r1225910628

We would like to ensure that we're consistent in how conversions from Python objects to Rust objects select for what's allowed.

I think the right approach would be to do something like https://github.com/PyO3/pyo3/blob/main/guide/src/conversions/tables.md where for each Rust type we state what Python types will convert to it (i.e. FromPyObject), and what Python type(s) it will convert back into (i.e. IntoPy<PyObject>).

That information does live on that page, but perhaps it can benefit from being rearranged.

In general we seem to be preferring conversions which are allow the user to be a bit loose with typing (e.g. string for IP address). So I think we should ensure we stick to this philosophy everywhere (e.g. maybe we should accept int for bool, or even all truthy types)?

We may also wish to consider a "strict" mode where only the exact Python type can be input (e.g. Python bool <-> Rust bool).

davidhewitt avatar Jun 11 '23 20:06 davidhewitt

Can I take it?

yusharth avatar Jul 29 '23 05:07 yusharth

Sure thing, please do! Is there any extra information you need to get started?

davidhewitt avatar Jul 29 '23 20:07 davidhewitt

Hey! If this is still open and something that requires improvement, I would be interested to a take look. Hopefully, I could contribute more in the future.

mikeleppane avatar Feb 04 '24 09:02 mikeleppane

Hello, thank you for your interest! Yes, this is absolutely still open to improvement.

First, I'd suggest starting with updating the conversions documentation in tables.md as I wrote in the original post above.

Second, if you're interested in going beyond collecting what's currently implemented to make things better, I think we would like to go down the path of having "default" conversions which allow a range of inputs and also a user-facing attribute to control when they want exactly a specified type.

I wrote about this topic most recently in https://github.com/PyO3/pyo3/pull/3638#issuecomment-1858583166

We called the option #[pyo3(noconvert)] in that PR.

davidhewitt avatar Feb 04 '24 14:02 davidhewitt