Use a safe and short repr in error messages and warning
See original GitHub issueWe print the value of an offending parameter in many of our error messages and warnings. However, when the object failing printing, or when its representation is too long, the resulting message is not useful.
We should:
- Use a safe_repr function to never fail printing (I am pasting an example below, with a test)
- Only print the 300 first characters (something like this) of it’s return, using a "short_repr’
These two functions should be added in the utils submodule and used in error messages and warnings in the codebase.
def safe_repr(value):
"""Hopefully pretty robust repr equivalent."""
# this is pretty horrible but should always return *something*
try:
return pydoc.text.repr(value)
except KeyboardInterrupt:
raise
except:
try:
return repr(value)
except KeyboardInterrupt:
raise
except:
try:
# all still in an except block so we catch
# getattr raising
name = getattr(value, '__name__', None)
if name:
# ick, recursion
return safe_repr(name)
klass = getattr(value, '__class__', None)
if klass:
return '%s instance' % safe_repr(klass)
except KeyboardInterrupt:
raise
except:
return 'UNRECOVERABLE REPR FAILURE'
def short_repr(obj):
msg = safe_repr(obj)
if len(msg) > 300:
return msg = '%s...' % msg
return msg
# For testing (in a test file, not in the same file)
class Vicious(object):
def __repr__(self):
raise ValueError
def test_safe_repr():
safe_repr(Vicious())
safe_repr is borrowed from joblib, but as it is not exposed in the public API, we shouldn’t import it from our vendored version of joblib (elsewhere, the “unvendoring” performed by debian will break the import)
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (10 by maintainers)
Top Results From Across the Web
Capture errors, warnings and messages - R-bloggers
To make a long story short: it is possible to capture... ... safely() ... return a simple list and capture, errors, warnings and...
Read more >Video: Input and error messages - Microsoft Support
Input and error messages · Select the cells that you want to create a message for, and click Data Validation. · On the...
Read more >Error Messages (The GNU C Library)
With the GNU C Library, the messages are fairly short; there are no multi-line messages or embedded newlines. Each error message begins with...
Read more >MDS User's Guide-Section 5 - QTSO - CMS
The Final Validation Report outlines the errors, whether fatal or simply warning, encountered in the submitted records. Each error or warning is noted...
Read more >How to Fix 'Be Careful With This Message' Error in Gmail (2022)
People might think your emails are spam or not trustworthy. · Recipients might hit the Report Spam or Report Phishing button in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
pprint is not robust to failing repr:
Hence, I do believe that the problem is not addressed.
While the above example can seem contrived, things like this can happen in repr of estimators in some rare cases.
There is probably some work to salvage from pull request #11601
The length of the error message may be improved now with default
print_changed_only=True
. We should confirm that our pprint is robust to errors before closing this.