diff --git a/fire/core.py b/fire/core.py index c1e97367..31f7cf1e 100644 --- a/fire/core.py +++ b/fire/core.py @@ -262,6 +262,13 @@ def _PrintResult(component_trace, verbose=False, serialize=None): print(str(result)) return + elif value_types.HasCustomRepr(result): + # Same as above, but for __repr__. + # For pandas.DataFrame, __str__ is inherited from object, but __repr__ has + # a custom implementation (see pandas.core.frame.DataFrame.__repr__) + print(str(result)) + return + if isinstance(result, (list, set, frozenset, types.GeneratorType)): for i in result: print(_OneLineResult(i)) diff --git a/fire/value_types.py b/fire/value_types.py index c0a137fd..f45d37b4 100644 --- a/fire/value_types.py +++ b/fire/value_types.py @@ -83,3 +83,21 @@ def HasCustomStr(component): if str_attr and str_attr.defining_class is not object: return True return False + +def HasCustomRepr(component): + """Reproduces above HasCustomStr function to determine if component has a + custom __repr__ method. + + ... + + Args: + component: The object to check for a custom __repr__ method. + Returns: + Whether `component` has a custom __repr__ method. + """ + if hasattr(component, '__repr__'): + class_attrs = inspectutils.GetClassAttrsDict(type(component)) or {} + repr_attr = class_attrs.get('__repr__') + if repr_attr and repr_attr.defining_class is not object: + return True + return False