pybind / pybind/pybind11

[BUG]: Pure virtual method overriding using mixin class gives: Tried to call pure virtual function …

Open
#5,405 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

2.13.5

Problem description

I cant override pure virtual functions using mixin classes. Pybind does not realize that the function in question does exist but is provided by the mixin class.

Reproducible example code
struct M {
    int f() { return 1; }
};

struct A {
    virtual int f() = 0;
};

// Imagine a trampoline class AT for A here

PYBIND11_MODULE(example, m) {
    pybind11::class_<M>(m, "Mixin")
        .def("f", &M::f);
    pybind11::class_<A, AT>(m, "Abstract")
        .def("f", &A::f);
}
class Derived(M, A):
    ...

d = D()
d.foo()

The last statement throws "Tried to call pure virtual function". For example in plain Python this works as expected:

class A():
    def f(self):
        pass

class M:
    def f(self):
        return "mixed in"

class D(M, A):
     pass

d = D()
print(d.f())
Is this a regression? Put the last known working version here if it is.

Not a regression

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 C++ mixin, abstract class, and trampoline setup in the reproducible example, then run the corresponding Python multiple-inheritance case. Trace how the pure virtual call is dispatched; done when the mixin-provided f() implementation is used instead of raising the pure-virtual error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.