0

Could someone give a full/working example of a function in pyO3 that takes a single input, and returns true/false depending on whether the input has type scipy.sparse.csr_matrix?

I'm aware that pyO3 has is_instance, but that seems to require having an object that represents a type. I know about get_type, but that assumes you already have an object of the right type, which gets me confused.

GHPR
  • 45
  • 6
  • Not too sure what you want exactly, if you want a Python-level typecheck you need to get a handle on the type you're looking for using [`import`](https://docs.rs/pyo3/latest/pyo3/marker/struct.Python.html#method.import) and something like [`getattr`](https://docs.rs/pyo3/latest/pyo3/types/struct.PyAny.html?search=import#method.getattr) to dereference the object you're looking for, then `is_instance` to do the actual check. – Masklinn May 30 '23 at 08:09
  • This can be simplified if pyo3 (or some other package) provides a Rust level version (then you can use `is_instance_of`, or even just request the rust-level type and let pyo3 do the typechecking) but unless someone provides an equivalent to [`rust-numpy`](https://github.com/PyO3/rust-numpy) that seems unlikely. – Masklinn May 30 '23 at 08:11
  • Thanks for this input. Could anyone give a concrete example? (of a function that takes a Python object as input and returns true/false depending on whether the Python object has a given type, e.g. `scipy.sparse.csr_matrix`) – GHPR Jun 01 '23 at 18:17

0 Answers0