-
Notifications
You must be signed in to change notification settings - Fork 804
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
WIP: Enhanced extract type errors (Issue #1640) #1649
WIP: Enhanced extract type errors (Issue #1640) #1649
Conversation
…sic structs and enums
… in FromPyObject::extract
…on tuples and newtype structs/tuples
…ted work on TypeError message on Enums
…ed with tests. Fixed formatting and linting issues
@batconjurer - thank you for working on this and sorry for my delay in reviewing. Reading the generated error message and thinking about this some more, I'd like to suggest a slightly denser format which I think is easier for the user to see what happened: Enum case:
Struct field:
You can get the error message without the error type using I'd prefer to avoid adding the As for why |
@davidhewitt I have made the suggested changes. A couple of comments
|
Thanks, I will re-review at the weekend. |
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.
@batconjurer I'm sorry this took me so long to re-review. If you've lost interest because of the delay, that's understandable, and I'm happy to take up finishing this off.
The acquire_gil
uses can definitely be avoided. I've added some comments on what to do.
I'm really happy with the enum error message.
I agree that the struct message is still too long.
New idea - we added PyErr::set_cause
in PyO3 0.14. Rather than formatting the old error into the new error, how about setting the original extraction error as the cause of the "failed to extract B.a" error?
That way it should break up across lines naturally thanks to Python.
@davidhewitt I am still interested in working on it. I will try to incorporate your comments as soon as I can. |
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
…catenating all error messages
@davidhewitt So I changed the structs to use the |
@batconjurer what David means is that you don't need to call |
Ok, I have removed the let py = unsafe{ Python::assume_gil_acquired() }; in all places |
Hmmm. I really would like us to avoid |
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.
👍 looking really great! I like the way these errors look now, thank you for the repeated iterations of this PR.
Just a couple of tiny nits and then the question of which safe way to get .py()
(maybe #1751 will be merged before long) and then I'd be keen to see this merged!
@batconjurer I don't understand:
is shorter than your solution and does not need to introduce unsafe code. |
Incorportated this. Did not realize at first that |
…emoved all unsafe code from macro generated code. Two other small fixes
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.
This is looking perfect. Finger crossed CI is green - if so, I think it's good to merge! Thanks for all your efforts!
Great, thanks! |
This PR includes allows the
FromPyObject
derive macro give more detailed error messages when conversion fails. It constructs a "traceback" of the nested types that failed the conversion to help more accurately pinpoint from where the type error originated.The following gives an examples for different complex types (REMEMBER TO CHANGE THIS WHEN FORMATTING IS FINALIZED):
Enum
This produces the following error message:
Note that for Enums, a reason for the failure of each variant is provided.
Struct
This produces the following error message (in the Python interpreter):
Transparent Struct
This produces the error message (in Python)
Tuple Struct
This produces the following error message: (in Python)
Transparent Tuple Struct
This produces the following error message (in Python):