How to accept a Python string or Python None
in a function
#2932
-
I've got a function that accepts two strings, but I'm wanting them to also be able to be set to fn my_function(arg1: Option<String>, arg2: Option<String>) { But this seems to make the arguments able to be omitted. I don't want the arguments to be able to be omitted though, I just want them to be nullable with Is there any way to do what I'm wanting? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Try adding a signature indicating that the arguments are mandatory and do not have a default value, e.g. #[pyfunction]
#[pyo3(signature=(arg1, arg2))]
fn my_function(arg1: Option<String>, arg2: Option<String>) { |
Beta Was this translation helpful? Give feedback.
-
That doesn't seem to be working, I'm thinking it might be the context I'm using it in, I'm using it in a class constructor like so: #[pyclass]
pub struct DistroArchPair {
pub distro: Option<String>,
pub arch: Option<String>
}
#[pymethods]
impl DistroArchPair {
#[new]
fn new(distro: Option<String>, arch: Option<String>) -> Self {
Self {
distro,
arch
}
}
} Adding
Sorry for being vague about it before, I just didn't want to send anything that complicated, but then I checked my code again and realized the affected part is quite tiny. |
Beta Was this translation helpful? Give feedback.
-
That was it, everything worked fine after upgrading my version. Thanks a ton! |
Beta Was this translation helpful? Give feedback.
-
For what it's worth, I'm adding a little bit of documentation for this case in #2934 |
Beta Was this translation helpful? Give feedback.
Try adding a signature indicating that the arguments are mandatory and do not have a default value, e.g.