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

Typing when manually dispatching #94

Open
lcnbr opened this issue Oct 25, 2024 · 1 comment
Open

Typing when manually dispatching #94

lcnbr opened this issue Oct 25, 2024 · 1 comment

Comments

@lcnbr
Copy link

lcnbr commented Oct 25, 2024

I have a function like this:

#[gen_stub_pyfunction(module="symbolica_community.tensors")]
#[pyfunction]
pub fn dense(structure: Bound<'_, PyAny>, data: Bound<'_, PyAny>) -> PyResult<Spensor> {
    let structure = PossiblyIndexed::extract_bound(&structure)?;

    if let Ok(d) = data.extract::<Vec<f64>>() {
        Ok(Spensor {
            tensor: DenseTensor::<f64, _>::from_data(d, structure)
                .map_err(|e| PyOverflowError::new_err(e.to_string()))?
                .into(),
        })
    } else if let Ok(d) = data.extract::<Vec<PythonExpression>>() {
        let data = d.into_iter().map(|e| e.expr).collect();
        Ok(Spensor {
            tensor: ParamOrConcrete::Param(ParamTensor::from(
                DenseTensor::<Atom, _>::from_data(data, structure)
                    .map_err(|e| PyOverflowError::new_err(e.to_string()))?,
            )),
        })
    } else {
        Err(PyTypeError::new_err("Only float type supported"))
    }
}

where I try different types for extracting. Is there any way to communicate the set of allowed types to pyo3-stub-gen?

@tvanbaak
Copy link
Contributor

tvanbaak commented Nov 1, 2024

Based on #82, it seems like there are some issues with function type signatures, but it's worth noting that if you impl PyStubType, you can return a union of TypeInfo. The crate does this to support a string-or-path type here: https://github.com/Jij-Inc/pyo3-stub-gen/blob/0.6.0/pyo3-stub-gen/src/stub_type/builtins.rs#L55-L57

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

No branches or pull requests

2 participants