-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[BUG] Allow for __str__ to be usable with enums (again) #2537
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
Comments
The default implementation of A work-around would be to avoid overloading and replace the default implementation. I think the shortest snippet for this would be pet.attr("__str__") = py::cpp_function(
[](Pet &self){ ... },
py::name("__str__"),
py::is_method(pet)
); This effectively repeats body of Probably overriding should be made less verbose than that (e.g. adding |
We could also just make the default |
I'm adding this to the 2.6.0 milestone, to see if we want to check this before that release. |
I believe this would be solvable with #1131 |
Also, yes. That would be a nice solution as well! :-) |
@henryiii Using @YannickJadoul Disabling default implementations with constructor flags might turn into combinatorical hell if user wants to override e.g. two out of three functions. Or maybe constructor flags should be parametrized with ignored functions... Looks like a hypotetical |
You're right about @henryiii's and my proposed solution, but I do feel we need a bit more time to consider the consequences of |
I would assume "py::override" would delete the call chain and then add itself? Maybe Isn't the 'bug' here that |
The call-chain itself always has runtime overhead, but selecting the first item in the chain should be the same, regardless of the length of the chain (1 vs. 2) (unless we optimize something somehow for length-1). The docstring, yes, probably would have two lines. |
One of the hardest problems in CS :) I'm not against Yes, I think |
FWIW, this workaround does the trick. Thanks! |
FWIW I like the solution via |
* Adds some mising Python bindings like UNSPEC enum values * Fix RANGE not being masked to 20 bits when parsing packets in Python * Fix different __str__ behavior for enums using newest pybind11: pybind/pybind11#2537 * Fix digest module using platform-specific bit widths for header fields causing spurious test failures when checking hashes Approved-by: Kairen Wong Approved-by: Chris Bayruns Approved-by: Pavlo Bashmakov
* Adds some mising Python bindings like UNSPEC enum values * Fix RANGE not being masked to 20 bits when parsing packets in Python * Fix different __str__ behavior for enums using newest pybind11: pybind/pybind11#2537 * Fix digest module using platform-specific bit widths for header fields causing spurious test failures when checking hashes Approved-by: Kairen Wong Approved-by: Chris Bayruns Approved-by: Pavlo Bashmakov
* Adds some mising Python bindings like UNSPEC enum values * Fix RANGE not being masked to 20 bits when parsing packets in Python * Fix different __str__ behavior for enums using newest pybind11: pybind/pybind11#2537 * Fix digest module using platform-specific bit widths for header fields causing spurious test failures when checking hashes Approved-by: Kairen Wong Approved-by: Chris Bayruns Approved-by: Pavlo Bashmakov
In version 2.4 the following code did the expected thing, i.e. invoked the defined
__str__
overload for the enum whenstr()
was used in Python code on an instance of that enum:This does not work anymore in V2.6 (I have not checked in V2.5, though). V2.6 always returns either
"Pet.Dog"
or"Pet.Cat"
and ignores the defined__str__
.The text was updated successfully, but these errors were encountered: