python-bindings
python-bindings copied to clipboard
Error message for python binding shows cpp method name
Error message while using python binding shows cpp method names instead of python snake-case name.
Describe your setup
Operating system (e.g. Linux distribution and version): Ubuntu 24.04.2 preCICE Version: 3.1.1
Add the versions/commit hashes of further adapter and/or bindings. pyprecice==3.1.2
Describe the problem
Step To Reproduce
- Run the solver-dummy with line56 commented.
Expected behaviour The error message should print the python method names (snake-case).
I am transferring this issue to the Python bindings repository, where I think it fits best.
I understand your viewpoint, coming from the Python API perspective. Probably we need to add some message in the exception handling in the Python bindings.
@kss682 can you explain a bit more about why you need this fixed? I'm just asking because maybe I'm missing some technical detail in your use case. I agree: It would clearly improve the usability of the bindings if we point to set_mesh_vertex instead of setMeshVertex. Even though this is important I would argue that this is not critical. (Please don't get me wrong: Thanks a lot for opening this issue, you are triggering an important discussion here)
A major complication comes from the fact that the error messages are generated by the C++ preCICE logger which does not know that the output will be given to a user of the Python bindings. Meaning: We would have to tell the logger that its output should comply with the Python API. However, this contradicts the design of the bindings (not only Python but also MATLAB, Julia, Rust...) that the C++ library does not have to know about the bindings.
At the moment I have the impression this is an issue with quite high (development & maintainment) effort + potentially some design pitfalls.
Actually, the Python bindings go through the C bindings, right? Then we cannot just handle exceptions.
I think preCICE does not need to know which function called it, that would be complicated. But maybe we can have an error message like this:
Error in write_data:
ERROR: The provided mesh ... Please set the mesh using setMeshVertex()/setMeshVertices() prior to calling initialize().
This would make it easier to locate the problem in the bindings (or the calling code), even if it just shows a preCICE error.
But I agree that we should not worry about the hard-coded names. Besides the cost-benefit issue, I think they help more than they confuse.
@BenjaminRodenberg I had noticed this while going through precice tutorials. The issue was raised more as improvement in readability of the error message as there was a moment when I was searching for the function name from the error in the python docs. Maybe the tag bug is wrong in this case.
A major complication comes from the fact that the error messages are generated by the C++ preCICE logger which does not know that the output will be given to a user of the Python bindings.
Correct. And it is already tricky enough to keep the error messages in the core library descriptive.
Actually, the Python bindings go through the C bindings, right? Then we cannot just handle exceptions.
The python bindings use Cython to interface with the C++ API. Exceptions are currently raised as python RuntimeError, which is how Cython transforms exceptions from the C++ realm.
As the python bindings already wrap the direct Cython C++ interface, there is already a layer which could be used to patch the error message of the exception itself.
Example
https://github.com/precice/python-bindings/blob/c28df2b64a504b276f8df518cd9cf443a406ef3f/cyprecice/cyprecice.pyx#L118
Could be
try:
self.thisptr.initialize()
except RuntimeError as e:
handleError(e)
where handleError takes the exception, updates the wording by replacing setMeshVertex with set_mesh_vertex, and raises either a RuntimeError or some precice.Error.
This however leads to different messages in the output emitted from the preCICE core library and the exception thrown by the python bindings. This could be confusing.
I like the suggestion of @fsimonis, sounds like "low effort some gain" to me. Keep in mind that through the course the Python bindings are often the first place where new users get to know preCICE.