Skip to content

[BUG]: C++ instance passed as pointer not marked as optional in function signature #3175

Open
@guillaumekln

Description

@guillaumekln

Required prerequisites

Problem description

When passing a C++ instance as a pointer, the default behaviour is to accept None as described in the documentation. Consider the following example:

py::class_<Dog>(m, "Dog").def(py::init<>());
m.def("bark",
      [](const Dog* dog) { return dog ? "woof!" : "(no dog)"; },
      py::arg("dog")=py::none());

The function signature in help(module) looks like this:

FUNCTIONS
    bark(...) method of builtins.PyCapsule instance
        bark(dog: module.Dog = None) -> str

but we would expect this signature instead:

FUNCTIONS
    bark(...) method of builtins.PyCapsule instance
        bark(dog: Optional[module.Dog] = None) -> str

I tested with pybind11 2.7.1.

Reproducible example code

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    signaturesIssue about static signatures

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions