(
f: Function, parameters: list[Parameter]
)
| 1339 | |
| 1340 | @staticmethod |
| 1341 | def format_docstring_signature( |
| 1342 | f: Function, parameters: list[Parameter] |
| 1343 | ) -> str: |
| 1344 | lines = [] |
| 1345 | lines.append(f.displayname) |
| 1346 | if f.forced_text_signature: |
| 1347 | lines.append(f.forced_text_signature) |
| 1348 | elif f.kind in {GETTER, SETTER}: |
| 1349 | # @getter and @setter do not need signatures like a method or a function. |
| 1350 | return '' |
| 1351 | else: |
| 1352 | lines.append('(') |
| 1353 | |
| 1354 | # populate "right_bracket_count" field for every parameter |
| 1355 | assert parameters, "We should always have a self parameter. " + repr(f) |
| 1356 | assert isinstance(parameters[0].converter, self_converter) |
| 1357 | # self is always positional-only. |
| 1358 | assert parameters[0].is_positional_only() |
| 1359 | assert parameters[0].right_bracket_count == 0 |
| 1360 | positional_only = True |
| 1361 | for p in parameters[1:]: |
| 1362 | if not p.is_positional_only(): |
| 1363 | positional_only = False |
| 1364 | else: |
| 1365 | assert positional_only |
| 1366 | if positional_only: |
| 1367 | p.right_bracket_count = abs(p.group) |
| 1368 | else: |
| 1369 | # don't put any right brackets around non-positional-only parameters, ever. |
| 1370 | p.right_bracket_count = 0 |
| 1371 | |
| 1372 | right_bracket_count = 0 |
| 1373 | |
| 1374 | def fix_right_bracket_count(desired: int) -> str: |
| 1375 | nonlocal right_bracket_count |
| 1376 | s = '' |
| 1377 | while right_bracket_count < desired: |
| 1378 | s += '[' |
| 1379 | right_bracket_count += 1 |
| 1380 | while right_bracket_count > desired: |
| 1381 | s += ']' |
| 1382 | right_bracket_count -= 1 |
| 1383 | return s |
| 1384 | |
| 1385 | need_slash = False |
| 1386 | added_slash = False |
| 1387 | need_a_trailing_slash = False |
| 1388 | |
| 1389 | # we only need a trailing slash: |
| 1390 | # * if this is not a "docstring_only" signature |
| 1391 | # * and if the last *shown* parameter is |
| 1392 | # positional only |
| 1393 | if not f.docstring_only: |
| 1394 | for p in reversed(parameters): |
| 1395 | if not p.converter.show_in_signature: |
| 1396 | continue |
| 1397 | if p.is_positional_only(): |
| 1398 | need_a_trailing_slash = True |
no test coverage detected