-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Enable defining custom __new__ #3265
Conversation
…urning pointer from "__init__" instead of reference.
How did I arrive at 194033e? When testing in the Google environment, I ran into this The error wasn't obvious at all, therefore I inserted this right before that
That way I got to see the While looking around, I saw that all new-style That's all I know. 🙂 |
Defining a custom new function for enums could help close #1177. Also we are putting a class method in the tests now so we might want to add a class method shim as mentioned in #1693 now to py_class as the current way of defining class methods is bulky and not very intuitive. We don't seem to have a way to check the py::type of a the C++ class for the first argument though, which is a bit annoying. |
This syntax is at the very least not terrible until we have proper classmethod support. |
I actually just realized those PTRs in test should probably be ref since they can't be null. Probably can go back and fix them when we write the actual documentation for this feature at some point @henryiii along with proper classmethod support perhaps |
|
||
py::class_<NoConstructor>(m, "NoConstructor") | ||
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance"); | ||
|
||
py::class_<NoConstructorNew>(m, "NoConstructorNew") | ||
.def(py::init([](NoConstructorNew *self) { return self; })) // Need a NOOP __init__ |
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 also wish there was a cleaner way to not have to implement __init__
if new is defined, but I can't figure out how to hack to the metaclass not to complain.
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.
Maybe something like .def_new(...)
or .def(py::new(...))
that wraps the two methods you have right now?
Description
Suggested changelog entry:
* Enable defining custom ``__new__`` methods on classes by fixing bug preventing overriding methods if they have non-pybind11 siblings.