pybind / pybind/pybind11

Pure virtual error message should include repr

Open
#2,106 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

It's not unlikely that consumers of libraries created with pybind11 are not fluent in C++. The current error message "Tried to call pure virtual function Class::function" is not obvious to non-C++ programmers. Additionally, if the function is being called by a C++ function that could potentially be calling many different objects, it can be difficult to find the right object that is missing the function.

Two ways to fix this:

  • Change the error message to include a repr of the object as python users might expect
  • Use different wording. Instead of saying 'pure virtual function', tell the user what they did wrong and (implicitly) how to fix it: "Foo does not override required function 'Class::method'

While getting the repr adds some overhead -- this is likely a programming error anyways, so extra overhead shouldn't be a concern.

My own implementation of this can be found at https://github.com/robotpy/robotpy-build/blob/24232e22de0c51f86b8a41476bed436803ddee2d/robotpy_build/include/robotpy_build.h#L15 , happy to contribute it if others find it useful.

This implementation results in the following error message:

RuntimeError: <__main__.PV object at 0x7f8bb820ab48> does not override required function "PureVirtual::missingFunction"

Or if the repr throws:

RuntimeError: <unknown> does not override required function "PureVirtual::missingFunction"

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 by reviewing the linked robotpy_build.h implementation and the pybind11 code that raises the pure-virtual error. Confirm how object repr failures should fall back to an unknown marker, then locate the relevant tests or add coverage for both successful and failing repr cases. Done means the error identifies the object and required function without breaking existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
developer-experience
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.