scikit-learn/scikit-learn
Voir sur GitHubUse a safe and short repr in error messages and warning
Open
#9 698 ouverte le 6 sept. 2017
Enhancementhelp wantedmodule:utils
Métriques du dépôt
- Stars
- (66 084 stars)
- Métriques de merge PR
- (Merge moyen 10j) (90 PRs mergées en 30 j)
Description
We 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)