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)
| 1174 | |
| 1175 | |
| 1176 | def _render_signature(obj_signature, obj_name) -> str: |
| 1177 | """ |
| 1178 | This was mostly taken from inspect.Signature.__str__. |
| 1179 | Look there for the comments. |
| 1180 | The only change is to add linebreaks when this gets too long. |
| 1181 | """ |
| 1182 | result = [] |
| 1183 | pos_only = False |
| 1184 | kw_only = True |
| 1185 | for param in obj_signature.parameters.values(): |
| 1186 | if param.kind == inspect.Parameter.POSITIONAL_ONLY: |
| 1187 | pos_only = True |
| 1188 | elif pos_only: |
| 1189 | result.append('/') |
| 1190 | pos_only = False |
| 1191 | |
| 1192 | if param.kind == inspect.Parameter.VAR_POSITIONAL: |
| 1193 | kw_only = False |
| 1194 | elif param.kind == inspect.Parameter.KEYWORD_ONLY and kw_only: |
| 1195 | result.append('*') |
| 1196 | kw_only = False |
| 1197 | |
| 1198 | result.append(str(param)) |
| 1199 | |
| 1200 | if pos_only: |
| 1201 | result.append('/') |
| 1202 | |
| 1203 | # add up name, parameters, braces (2), and commas |
| 1204 | if len(obj_name) + sum(len(r) + 2 for r in result) > 75: |
| 1205 | # This doesn’t fit behind “Signature: ” in an inspect window. |
| 1206 | rendered = '{}(\n{})'.format(obj_name, ''.join( |
| 1207 | ' {},\n'.format(r) for r in result) |
| 1208 | ) |
| 1209 | else: |
| 1210 | rendered = '{}({})'.format(obj_name, ', '.join(result)) |
| 1211 | |
| 1212 | if obj_signature.return_annotation is not inspect._empty: |
| 1213 | anno = inspect.formatannotation(obj_signature.return_annotation) |
| 1214 | rendered += ' -> {}'.format(anno) |
| 1215 | |
| 1216 | return rendered |