mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +00:00
Avoid attr("__repr__") in initialize_generic (#2317)
If the default argument value is a class, and not an instance of a
class, `a.value.attr("__repr__")` raises a `ValueError`. Switching to
`repr(a.value)` makes this use case work.
Fixes #2028
This commit is contained in:
@@ -123,4 +123,9 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
|
||||
py::class_<KWClass>(m, "KWClass")
|
||||
.def("foo0", &KWClass::foo)
|
||||
.def("foo1", &KWClass::foo, "x"_a, "y"_a);
|
||||
|
||||
// Make sure a class (not an instance) can be used as a default argument.
|
||||
// The return value doesn't matter, only that the module is importable.
|
||||
m.def("class_default_argument", [](py::object a) { return py::repr(a); },
|
||||
"a"_a = py::module::import("decimal").attr("Decimal"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user