pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.39k stars 2.07k forks source link

[BUG]: Linebreaks in docstring signature caused by default values #4734

Closed JeanElsner closed 1 year ago

JeanElsner commented 1 year ago

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

5ccb9e4

Problem description

If the representation of default values includes linebreaks, the function signature added to the docstring will include those linebreaks. Multiple tools only parse the docstring line by line for a signature (Sphinx, pybind11-stubgen), so this will break otherwise functional compatibility. For me this commonly happens with Eigen matrix arguments represented as Numpy arrays. If the default value representation of the Numpy array exceeds 70 characters or has more than one dimension, linebreaks will be printed. Simply stripping newlines before adding the signature to the function record fixed the issue for me, but I'm not sure this behavior is desired (cf. https://github.com/pybind/pybind11/pull/2621#issuecomment-774518438).

Reproducible example code

#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>

#include "Eigen/Dense"

namespace py = pybind11;

PYBIND11_MODULE(_core, m) {
  m.def(
      "test", [](Eigen::Matrix3d mat) {},
      py::arg("mat") = Eigen::Matrix3d::Identity());
}

This will produce the docstring:

test(mat: numpy.ndarray[numpy.float64[3, 3]] = array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])) -> None

Is this a regression? Put the last known working version here if it is.

Not a regression