MCPcopy Index your code
hub / github.com/python/mypy / parse_signature

Function parse_signature

mypy/stubdoc.py:444–478  ·  view source on GitHub ↗

Split function signature into its name, positional an optional arguments. The expected format is "func_name(arg, opt_arg=False)". Return the name of function and lists of positional and optional argument names.

(sig: str)

Source from the content-addressed store, hash-verified

442
443
444def parse_signature(sig: str) -> tuple[str, list[str], list[str]] | None:
445 """Split function signature into its name, positional an optional arguments.
446
447 The expected format is "func_name(arg, opt_arg=False)". Return the name of function
448 and lists of positional and optional argument names.
449 """
450 m = re.match(r"([.a-zA-Z0-9_]+)\(([^)]*)\)", sig)
451 if not m:
452 return None
453 name = m.group(1)
454 name = name.split(".")[-1]
455 arg_string = m.group(2)
456 if not arg_string.strip():
457 # Simple case -- no arguments.
458 return name, [], []
459
460 args = [arg.strip() for arg in arg_string.split(",")]
461 positional = []
462 optional = []
463 i = 0
464 while i < len(args):
465 # Accept optional arguments as in both formats: x=None and [x].
466 if args[i].startswith("[") or "=" in args[i]:
467 break
468 positional.append(args[i].rstrip("["))
469 i += 1
470 if args[i - 1].endswith("["):
471 break
472 while i < len(args):
473 arg = args[i]
474 arg = arg.strip("[]")
475 arg = arg.split("=")[0]
476 optional.append(arg)
477 i += 1
478 return name, positional, optional
479
480
481def build_signature(positional: Sequence[str], optional: Sequence[str]) -> str:

Callers 2

parse_all_signaturesFunction · 0.85

Calls 8

lenFunction · 0.85
groupMethod · 0.80
splitMethod · 0.80
stripMethod · 0.80
appendMethod · 0.80
rstripMethod · 0.80
startswithMethod · 0.45
endswithMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…