-
Notifications
You must be signed in to change notification settings - Fork 271
Throw TypeError when subclasses forget to call __init__ #1224
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
base: master
Are you sure you want to change the base?
Conversation
virtuald
commented
Nov 24, 2025
- Based on Throw TypeError when subclasses forget to call __init__ pybind/pybind11#2152
- Fixes [BUG]: nanobind fails with unhelpful error message when super.__init__ isn't called by overridden constructor #1210
|
|
||
| with pytest.warns( | ||
| RuntimeWarning, | ||
| match="nanobind: attempted to access an uninitialized instance of type", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any other test that checks for this string, but the place where the documentation discusses it is talking about a unique_ptr ownership transfer, which isn't this?
| { Py_tp_dealloc, (void *) nb_type_dealloc }, | ||
| { Py_tp_setattro, (void *) nb_type_setattro }, | ||
| { Py_tp_init, (void *) nb_type_init }, | ||
| { Py_tp_call, (void *) nb_type_call }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a vectorcall instead?
|
I am not opposed to improving the error message, but this seems to complex of a change to achieve that goal. It also has a performance cost even in the non-erroneous case. |
831784f to
04a20d3
Compare
|
You're right, I was overthinking it. Now the extra overhead is just an single comparison. |
|
Never mind, my local test environment was stale when I tested it. I'll take a closer look tonight. |
04a20d3 to
dbdb24a
Compare
|
Well, I got rid of the MRO stuff so it's a lot simpler now. I feel like the other performance improvement would be to convert this tp_call to vectorcall, but I haven't been successful at that yet. Edit: Ok, I got it using vectorcall when not using the limited API in Python 3.13.. and it looks like |
|
Instead of the current approach, how about a warning ( |
477f67b to
75f7046
Compare
|
Any news on this @virtuald ? |
|
I need to go back and check, but I'm pretty sure there's already a warning in the type caster when it encounters an uninitialized nanobind object. I don't think that's good enough, better to make it an exception -- and also, in my development experience I rarely encounter warnings because I forget to enable whatever setting it is that shows them, so I'm not convinced that it would be useful for beginner developers consuming a nanobind wrapped library. Regarding your performance concerns, it seems that if we could get it to only be installed when someone is inheriting from a class (instead of every nano-bound instance) that would be a good tradeoff? Alternatively, this could be an opt-in thing that is enabled by a I'll definitely revisit this in the future, but it might be a few weeks. |
1235f9a to
4ba51fc
Compare