##// END OF EJS Templates
Render signatures ourselves if they’re too long...
Philipp A -
Show More
@@ -362,7 +362,7 b' class Inspector(Colorable):'
362 If any exception is generated, None is returned instead and the
362 If any exception is generated, None is returned instead and the
363 exception is suppressed."""
363 exception is suppressed."""
364 try:
364 try:
365 hdef = oname + str(signature(obj))
365 hdef = render_signature(signature(obj), oname)
366 return cast_unicode(hdef)
366 return cast_unicode(hdef)
367 except:
367 except:
368 return None
368 return None
@@ -1016,3 +1016,47 b' class Inspector(Colorable):'
1016 search_result.update(tmp_res)
1016 search_result.update(tmp_res)
1017
1017
1018 page.page('\n'.join(sorted(search_result)))
1018 page.page('\n'.join(sorted(search_result)))
1019
1020
1021 def render_signature(obj, oname):
1022 """
1023 This was mostly taken from inspect.Signature.__str__.
1024 Look there for the comments.
1025 The only change is to add linebreaks when this gets too long.
1026 """
1027 if not isinstance(obj, inspect.Signature):
1028 return oname + str(obj)
1029
1030 result = []
1031 pos_only = False
1032 kw_only = True
1033 for param in self.parameters.values():
1034 if param.kind == _POSITIONAL_ONLY:
1035 pos_only = True
1036 elif pos_only:
1037 result.append('/')
1038 pos_only = False
1039
1040 if param.kind == _VAR_POSITIONAL:
1041 kw_only = False
1042 elif param.kind == _KEYWORD_ONLY and kw_only:
1043 result.append('*')
1044 kw_only = False
1045
1046 result.append(str(param))
1047
1048 if pos_only:
1049 result.append('/')
1050
1051 # add up name, parameters, braces (2), and commas
1052 if len(oname) + sum(len(r) + 2 for r in result) > 75:
1053 # This doesn’t fit behind “Signature: ” in an inspect window.
1054 rendered = '(\n{})'.format(''.join(' {},\n'.format(result)))
1055 else:
1056 rendered = '({})'.format(', '.join(result))
1057
1058 if self.return_annotation is not _empty:
1059 anno = formatannotation(self.return_annotation)
1060 rendered += ' -> {}'.format(anno)
1061
1062 return rendered
General Comments 0
You need to be logged in to leave comments. Login now