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.
(obj_signature, obj_name)
| 989 | |
| 990 | |
| 991 | def _render_signature(obj_signature, obj_name) -> str: |
| 992 | """ |
| 993 | This was mostly taken from inspect.Signature.__str__. |
| 994 | Look there for the comments. |
| 995 | The only change is to add linebreaks when this gets too long. |
| 996 | """ |
| 997 | result = [] |
| 998 | pos_only = False |
| 999 | kw_only = True |
| 1000 | for param in obj_signature.parameters.values(): |
| 1001 | if param.kind == inspect._POSITIONAL_ONLY: |
| 1002 | pos_only = True |
| 1003 | elif pos_only: |
| 1004 | result.append('/') |
| 1005 | pos_only = False |
| 1006 | |
| 1007 | if param.kind == inspect._VAR_POSITIONAL: |
| 1008 | kw_only = False |
| 1009 | elif param.kind == inspect._KEYWORD_ONLY and kw_only: |
| 1010 | result.append('*') |
| 1011 | kw_only = False |
| 1012 | |
| 1013 | result.append(str(param)) |
| 1014 | |
| 1015 | if pos_only: |
| 1016 | result.append('/') |
| 1017 | |
| 1018 | # add up name, parameters, braces (2), and commas |
| 1019 | if len(obj_name) + sum(len(r) + 2 for r in result) > 75: |
| 1020 | # This doesn’t fit behind “Signature: ” in an inspect window. |
| 1021 | rendered = '{}(\n{})'.format(obj_name, ''.join( |
| 1022 | ' {},\n'.format(r) for r in result) |
| 1023 | ) |
| 1024 | else: |
| 1025 | rendered = '{}({})'.format(obj_name, ', '.join(result)) |
| 1026 | |
| 1027 | if obj_signature.return_annotation is not inspect._empty: |
| 1028 | anno = inspect.formatannotation(obj_signature.return_annotation) |
| 1029 | rendered += ' -> {}'.format(anno) |
| 1030 | |
| 1031 | return rendered |