Skip to content

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

Closed
@JeanElsner

Description

@JeanElsner

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. #2621 (comment)).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions