Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message when inheriting from a type without a constructor #2432

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,14 @@ extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, P
// Ensure that the base __init__ function(s) were called
for (const auto &vh : values_and_holders(instance)) {
if (!vh.holder_constructed()) {
PyErr_Format(PyExc_TypeError, "%.200s.__init__() must be called when overriding __init__",
vh.type->type->tp_name);
auto message = "%.200s.__init__() must be called when overriding __init__";
#if !defined(PYPY_VERSION)
auto default_init = ((PyHeapTypeObject*)get_internals().instance_base)->ht_type.tp_init;
if (vh.type->type->tp_init == default_init) {
message = "%.200s has no __init__ and cannot be used as a base class from Python";
}
#endif
PyErr_Format(PyExc_TypeError, message, vh.type->type->tp_name);
Py_DECREF(self);
return nullptr;
}
Expand Down Expand Up @@ -620,7 +626,7 @@ inline PyObject* make_new_python_type(const type_record &rec) {
type->tp_bases = bases.release().ptr();

/* Don't inherit base __init__ */
type->tp_init = pybind11_object_init;
type->tp_init = ((PyHeapTypeObject*)internals.instance_base)->ht_type.tp_init;

/* Supported protocols */
type->tp_as_number = &heap_type->as_number;
Expand Down
31 changes: 25 additions & 6 deletions tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ def __init__(self):
pass
with pytest.raises(TypeError) as exc_info:
Python()
expected = ["m.class_.Pet.__init__() must be called when overriding __init__",
"Pet.__init__() must be called when overriding __init__"] # PyPy?
if env.PYPY:
virtuald marked this conversation as resolved.
Show resolved Hide resolved
expected = "Pet.__init__() must be called when overriding __init__"
else:
expected = "m.class_.Pet.__init__() must be called when overriding __init__"
# TODO: fix PyPy error message wrt. tp_name/__qualname__?
assert msg(exc_info.value) in expected
assert msg(exc_info.value) == expected

# Multiple bases
class RabbitHamster(m.Rabbit, m.Hamster):
Expand All @@ -124,9 +126,26 @@ def __init__(self):

with pytest.raises(TypeError) as exc_info:
RabbitHamster()
expected = ["m.class_.Hamster.__init__() must be called when overriding __init__",
"Hamster.__init__() must be called when overriding __init__"] # PyPy
assert msg(exc_info.value) in expected
if env.PYPY:
expected = "Hamster.__init__() must be called when overriding __init__"
else:
expected = "m.class_.Hamster.__init__() must be called when overriding __init__"
assert msg(exc_info.value) == expected

# Base doesn't have __init__
class ChChimera(m.Chimera):
def __init__(self):
pass

with pytest.raises(TypeError) as exc_info:
ChChimera()
if env.PYPY:
# can't detect no __init__ in PyPy
expected = "Chimera.__init__() must be called when overriding __init__"
else:
expected = \
"m.class_.Chimera has no __init__ and cannot be used as a base class from Python"
assert msg(exc_info.value) == expected


def test_automatic_upcasting():
Expand Down