Is it possible to configure PyCharm to use a custom function to display the __str__ representation of a type in a debug session? I am referring to built-in types or types imported from third party libraries which I would rather not modify.
For example, instead of a string in the debugger like {lxml.html.HtmlElement} <Element tr at 0x10e2c1418> I would like to have the output of etree.tostring(element).
Intellij Idea has Java Type Renderers where you can set a custom toString() method for any type, so that during debugging those types will use your custom toString() renderers. Is similar functionality available or achievable in PyCharm?
I tried this approach:
# for lxml.html
lxml.html.HtmlElement.__str__ = lxml.html.etree.tostring
This gives the expected result for lxml.html, but it feels like an ugly workaround, and I would like to find a way to do it that does not require monkey-patching external libraries.
The downside of this approach is that it doesn't work for example with lxml.etree.Element because setting lxml.etree.Element.__str__ = lxml.etree.tostring has no effect, as it delegates to lxml.etree._Element which is C native module with a read-only __str__.
There is no such feature for Python, please vote for the corresponding ticket in PyCharms' bug tracker: PY-21984.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With