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

Method _get_func_args

mypy/stubgen.py:584–648  ·  view source on GitHub ↗
(self, o: FuncDef, ctx: FunctionContext)

Source from the content-addressed store, hash-verified

582 return FunctionSig(func_def.name, args, retname, type_args)
583
584 def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]:
585 args: list[ArgSig] = []
586
587 # Ignore pos-only status of magic methods whose args names are elided by mypy at parse
588 actually_pos_only_args = o.name not in MAGIC_METHODS_POS_ARGS_ONLY
589 pos_only_marker_position = 0 # Where to insert "/", if any
590 for i, arg_ in enumerate(o.arguments):
591 var = arg_.variable
592 kind = arg_.kind
593 name = var.name
594 annotated_type = (
595 o.unanalyzed_type.arg_types[i]
596 if isinstance(o.unanalyzed_type, CallableType)
597 else None
598 )
599 # I think the name check is incorrect: there are libraries which
600 # name their 0th argument other than self/cls
601 is_self_arg = i == 0 and name == "self"
602 is_cls_arg = i == 0 and name == "cls"
603 typename: str | None = None
604 if annotated_type and not is_self_arg and not is_cls_arg:
605 # Luckily, an argument explicitly annotated with "Any" has
606 # type "UnboundType" and will not match.
607 if not isinstance(get_proper_type(annotated_type), AnyType):
608 typename = self.print_annotation(annotated_type)
609
610 if actually_pos_only_args and arg_.pos_only:
611 pos_only_marker_position += 1
612
613 if kind.is_named() and not any(arg.name.startswith("*") for arg in args):
614 args.append(ArgSig("*"))
615
616 default = "..."
617 if arg_.initializer:
618 if not typename:
619 typename = self.get_str_type_of_node(arg_.initializer, can_be_incomplete=False)
620 potential_default, valid = self.get_str_default_of_node(arg_.initializer)
621 if valid and len(potential_default) <= 200:
622 default = potential_default
623 elif kind == ARG_STAR:
624 name = f"*{name}"
625 elif kind == ARG_STAR2:
626 name = f"**{name}"
627
628 args.append(
629 ArgSig(name, typename, default=bool(arg_.initializer), default_value=default)
630 )
631 if pos_only_marker_position:
632 args.insert(pos_only_marker_position, ArgSig("/"))
633
634 if ctx.class_info is not None and all(
635 arg.type is None and arg.default is False for arg in args
636 ):
637 new_args = infer_method_arg_types(
638 ctx.name, ctx.class_info.self_var, [arg.name for arg in args]
639 )
640
641 if ctx.name == "__exit__":

Callers 1

Calls 15

get_str_type_of_nodeMethod · 0.95
get_proper_typeFunction · 0.90
ArgSigClass · 0.90
infer_method_arg_typesFunction · 0.90
enumerateFunction · 0.85
isinstanceFunction · 0.85
anyFunction · 0.85
lenFunction · 0.85
boolClass · 0.85
allFunction · 0.85
print_annotationMethod · 0.80

Tested by

no test coverage detected