Closed marcoffee closed 4 years ago
The error message here is not precise. You have bad default value in signature and this triggers the error message:
def test_function(enum_val: sdutil.test_module.TestEnum = <TestEnum.VALUE_1: 0>) -> None
Since https://github.com/pybind/pybind11/pull/2126 the repr()
returns <>
-enclosed string which is not valid python expression and can't be used as default value. You can fix it by providing custom representation of default value via py::arg_v
[doc] or (re-)defining repr
/str
for enum class.
Note: your binding code is correct from runtime perspective, pybind11-stubgen
complaints only about docstrings it finds within the module.
Thanks for the help!
Just for the record, I simply replaced the line py::arg("enum_val") = TestEnum::VALUE_1
with py::arg_v("enum_val", TestEnum::VALUE_1, "TestEnum.VALUE_1")
and now it works fine.
I also found a way to circumvent some of those cases by adapting the function replace_default_pybind11_repr_with_ellipses
. Can I create a pull request for this fix?
IMO binding code should to be as correct as possible, including generated docstrings. py::arg_v
is the right tool to fix default value representation in signatures. While pybind11-stubgen
can apply some fix-ups it's a non-goal of the project.
PRs are well appreciated, but I think here I have a patch (https://github.com/sizmailov/pybind11-stubgen/commit/311b8e6f7a51189559134b0d9002a8f77fe4e2d6) which does what you've planned.
The patch test reveals another issue https://github.com/sizmailov/pybind11-stubgen/commit/311b8e6f7a51189559134b0d9002a8f77fe4e2d6#r43237564, but I think I'll merge it anyway.
I think that this patch is slightly better than my solution (b1a4b1b1fd7c77ebb18cfec6347032f2845e1d51), as it avoids searching the string twice. In addition, my solution also reveals this problem. If you need any help with this matter, let me know.
When I try to use an (previously defined via
pybind11::enum_
)enum
as default value for apybind11::arg
,pybind11-stubgen
thinks it is a C++ type. Below is an example:After installing it as
sdutil.test_module
viapip
and runningpybind11-stubgen sdutil.test_module
, I get the following errors:When I cast
TestEnum::VALUE_1
toint8_t
, the stubs are generated, but when I call the function withoutenum_val
parameter,pybind11
complains that the type is invalid.Thanks in advance!