Closed juanjosegarciaripoll closed 1 month ago
Please always provide a minimal complete reproducer that I can compile on my end. This snippet isn't enough to investigate. I am closing this for now. What I suspect is that you forgot an nb::arg().none()
annotation.
Here's a minimal example. First the C++ file (I have stripped some code)
#include <nanobind/nanobind.h>
// core.cc
NB_MODULE(core, m) {
class A {
double x{0.0};
public:
A(nb::object data) : x{nb::cast<double>(data)} {};
};
class B : public A {
double y{0.1};
public:
B(nb::object data, double ydata, nb::object rescale) : A(data), y{ydata} {
if (!rescale.is_none()) {
y /= nb::cast<double>(rescale);
}
}
};
nb::class_<A>(m, "A").def(nb::init<nb::object>());
nb::class_<B, A>(m, "B").def(nb::init<nb::object, double, nb::object>(),
"data"_a, "error"_a = 0.0, "rescale"_a.none());
}
Here is the Python file
from seemps.state.core import A, B
data = B(1.0, 0.0)
print(data)
And here's the error message. Obviously there's some problem in the argument parsing code, because instead of setting None
to the missing optional argument, the value of the last argument is copied.
(nanobind) PS C:\Users\juanj\src\seemps5> python foo.py
Traceback (most recent call last):
File "C:\Users\juanj\src\seemps5\foo.py", line 3, in <module>
data = B(1.0, 0.0)
^^^^^^^^^^^
TypeError: __init__(): incompatible function arguments. The following argument types are supported:
1. __init__(self, data: object, error: float = 0.0, rescale: object | None) -> None
Invoked with types: seemps.state.core.B, float, float
In this particular example, if I replace "rescale"_a.none()
with "rescale"_a = nb::none()
, the example works. However, when I do this in the code where I had the original problem, there is an obscure cast error
.
Problem description
I have a function with signature
that takes care of parsing various possible input types. In particular, I need every of these values is allowed to be
nb::none
, in case a given object parameter is not to be changed. The problem is that this function rejects arguments that are, for instance, of an enum type created by me in nanobind. This leads to very ugly error messages of the kindReproducible example code
No response