pybind / pybind/pybind11

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

Open
#3,175 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

signatures
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the documented allow/prohibiting None behavior and reproduce the supplied m.def("bark", ...) example, then inspect how help(module) renders the pointer argument in the function signature. Done means the signature shows Optional[module.Dog] for the nullable pointer while preserving the default None behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.