Convert function signature to list of FunctionSig Look for function signatures of function in docstring. Signature is a string of the format ( ) -> or perhaps without the return type. Returns empty list, when no signature is found, one signatu
(docstr: str | None, name: str)
| 383 | |
| 384 | |
| 385 | def infer_sig_from_docstring(docstr: str | None, name: str) -> list[FunctionSig] | None: |
| 386 | """Convert function signature to list of FunctionSig |
| 387 | |
| 388 | Look for function signatures of function in docstring. Signature is a string of |
| 389 | the format <function_name>(<signature>) -> <return type> or perhaps without |
| 390 | the return type. |
| 391 | |
| 392 | Returns empty list, when no signature is found, one signature in typical case, |
| 393 | multiple signatures, if docstring specifies multiple signatures for overload functions. |
| 394 | Return None if the docstring is empty. |
| 395 | |
| 396 | Arguments: |
| 397 | * docstr: docstring |
| 398 | * name: name of function for which signatures are to be found |
| 399 | """ |
| 400 | if not (isinstance(docstr, str) and docstr): |
| 401 | return None |
| 402 | |
| 403 | state = DocStringParser(name) |
| 404 | # Return all found signatures, even if there is a parse error after some are found. |
| 405 | with contextlib.suppress(tokenize.TokenError): |
| 406 | try: |
| 407 | tokens = tokenize.tokenize(io.BytesIO(docstr.encode("utf-8")).readline) |
| 408 | for token in tokens: |
| 409 | state.add_token(token) |
| 410 | except IndentationError: |
| 411 | return None |
| 412 | sigs = state.get_signatures() |
| 413 | |
| 414 | def is_unique_args(sig: FunctionSig) -> bool: |
| 415 | """return true if function argument names are unique""" |
| 416 | return len(sig.args) == len({arg.name for arg in sig.args}) |
| 417 | |
| 418 | # Return only signatures that have unique argument names. Mypy fails on non-unique arg names. |
| 419 | return [sig for sig in sigs if is_unique_args(sig)] |
| 420 | |
| 421 | |
| 422 | def infer_arg_sig_from_anon_docstring(docstr: str) -> list[ArgSig]: |
searching dependent graphs…