From 999c7e8eb6264bab1a00d02bf6097037d5ac1c0f 2018-11-21 10:45:44 From: Philipp A Date: 2018-11-21 10:45:44 Subject: [PATCH] Render signatures ourselves if they’re too long Fixes #11504 --- diff --git a/IPython/core/oinspect.py b/IPython/core/oinspect.py index 98f3fd7..2e97ed7 100644 --- a/IPython/core/oinspect.py +++ b/IPython/core/oinspect.py @@ -362,7 +362,7 @@ class Inspector(Colorable): If any exception is generated, None is returned instead and the exception is suppressed.""" try: - hdef = oname + str(signature(obj)) + hdef = render_signature(signature(obj), oname) return cast_unicode(hdef) except: return None @@ -1016,3 +1016,47 @@ class Inspector(Colorable): search_result.update(tmp_res) page.page('\n'.join(sorted(search_result))) + + +def render_signature(obj, oname): + """ + This was mostly taken from inspect.Signature.__str__. + Look there for the comments. + The only change is to add linebreaks when this gets too long. + """ + if not isinstance(obj, inspect.Signature): + return oname + str(obj) + + result = [] + pos_only = False + kw_only = True + for param in self.parameters.values(): + if param.kind == _POSITIONAL_ONLY: + pos_only = True + elif pos_only: + result.append('/') + pos_only = False + + if param.kind == _VAR_POSITIONAL: + kw_only = False + elif param.kind == _KEYWORD_ONLY and kw_only: + result.append('*') + kw_only = False + + result.append(str(param)) + + if pos_only: + result.append('/') + + # add up name, parameters, braces (2), and commas + if len(oname) + sum(len(r) + 2 for r in result) > 75: + # This doesn’t fit behind “Signature: ” in an inspect window. + rendered = '(\n{})'.format(''.join(' {},\n'.format(result))) + else: + rendered = '({})'.format(', '.join(result)) + + if self.return_annotation is not _empty: + anno = formatannotation(self.return_annotation) + rendered += ' -> {}'.format(anno) + + return rendered