-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Extended options object to choose between the print of __repr__ and __str__ (edited title) #901
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
Conversation
update from pybind11 to my fork
…ble function arguments.
|
Unfortunately, neither |
|
Also, for making this configurable, a define is discouraged—it's easy to cause an ODR violation if you link together different compilation units which don't have exact feature macro matches. If you do want to write up support for showing |
|
I'll try to implement it as suggested using and commit the results the next days |
…ould be used if argmuments mismatch
|
I used More suggestions? |
|
I'd prefer the names |
|
So |
|
Ah, right, I forgot about the conflicting getter. How about: |
|
Fine ! commit is coming in a sec |
|
Anything else I can do to accept this PR? |
|
Some suggestions:
py::class_<C> c(m, "C");
{
py::options options;
options.set_type_error_print_str();
c.def("f", [](ArgType a) { /* ... */ }); // Uses str() on argument failure
}
c.def("g", [](ArgType a) { /* ... */ }); // Uses repr() on argument failureThe problem is that once you're out of the scope, the option gets reset to default, so you need to preserve the option as part of the function definition at the time the definition is set. To do this, you'll need to add a
|
|
Thanks for the detailed instructions! |
|
Long messages for arrays are annoying, but I'm not sure that switching to import numpy as np
def str_vs_repr(n):
a = np.arange(n)
print(str(a))
print(repr(a))For Perhaps it would be better to leave |
|
@dean0x7d good suggestion. I would suggest the following: |
|
Another option that might be valuable: displaying the argument type name, rather than either |
|
So in our usecases with https://github.com/DerThorsten/nifty/ we get the TypeError because we called the function with the wrong arguments. Therefore I really like your suggestion |
|
Okay, forget the |
|
Ok, you get a PR soon (but most probably not today) |
|
I'm not really a fan of making everything an option. Good defaults are very important. An option should be added only if there is a compelling reason. For |
|
Also fine. @jagerman feel free to decide, I'll implement any solution since I really need this to be fixed =) |
|
I don't feel particularly strongly either way, so I guess that means go with the truncated |
|
I am not so sure about this PR. It's possible to extend pybind11 in a myriad of different ways to address every one-off requirement that users of this project might have, which will make everyone happy in the short term, but which would lead to significant bloat eventually. When we accept PRs, they should provide a clear improvement that helps more than just one or two users. This one IMO seems below the threshold. |
|
@wjakob - right; I think we settled on just truncating the |
|
@jagerman I impemented the trucation and add I think this message could reduce confusing. Are you fine with this impl. ATM I use 100 as a size limit which could be to small. Suggestions? Should I open a new PR or should we stick to this one? Greetings |
|
Stick to this PR.
// Returns the string size (in unicode codepoints, *not* bytes)
size_t size() { return (size_t) PyUnicode_GET_LENGTH(m_ptr); }and then in the "invoked with" code use something like this for the shortening logic: auto arg_repr = pybind11::repr(args_[ti]);
std::string arg =
#ifdef NDEBUG
arg_repr.size() <= 100 ? (std::string) arg_repr : (std::string) pybind11::str("{!r:.95}").format(arg_repr) + "[...]";
#else
arg_repr;
#endifI'm using 100 for the sake of example: I think we could shorten it to, e.g., 50 (with 45 in the format). We could also use just "..." instead of "[...]", and change the format size value to I think it might also be worthwhile only when |
|
@DerThorsten - ping, any updates on this? |
|
I took another look at this and just don't think that this is important enough to warrant the necessary changes. Sorry, it seems like you spent quite a bit of time on this. |
pybind11 will raise a TypeError if the exported classes / functions are called with incompatible function arguments.
In the cpp impl
pybind11::repr/__repr__is called, but I would strongly suggest to callpybind::str/__str_.__repr__can get very very long for array like classes.The printout can take very long and furthermore it does not help the user very much.
One could also something like:
If this is preferred, I could implement this.
But I can see almost no benefit to just using
__str__