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

Method generate_function_stub

mypy/stubgenc.py:606–655  ·  view source on GitHub ↗

Generate stub for a single function or method. The result (always a single line) will be appended to 'output'. If necessary, any required names will be added to 'imports'. The 'class_name' is used to find signature of __init__ or __new__ in 'class_sigs'.

(
        self, name: str, obj: object, *, output: list[str], class_info: ClassInfo | None = None
    )

Source from the content-addressed store, hash-verified

604 inferred[i] = sig._replace(ret_type=self.strip_or_import(sig.ret_type))
605
606 def generate_function_stub(
607 self, name: str, obj: object, *, output: list[str], class_info: ClassInfo | None = None
608 ) -> None:
609 """Generate stub for a single function or method.
610
611 The result (always a single line) will be appended to 'output'.
612 If necessary, any required names will be added to 'imports'.
613 The 'class_name' is used to find signature of __init__ or __new__ in
614 'class_sigs'.
615 """
616 docstring: Any = getattr(obj, "__doc__", None)
617 if not isinstance(docstring, str):
618 docstring = None
619
620 ctx = FunctionContext(
621 self.module_name,
622 name,
623 docstring=docstring,
624 is_abstract=self.is_abstract_method(obj),
625 class_info=class_info,
626 )
627 if self.is_private_name(name, ctx.fullname) or self.is_not_in_all(name):
628 return
629
630 self.record_name(ctx.name)
631 default_sig = self.get_default_function_sig(obj, ctx)
632 inferred = self.get_signatures(default_sig, self.sig_generators, ctx)
633 self.process_inferred_sigs(inferred)
634
635 decorators = []
636 if len(inferred) > 1:
637 decorators.append("@{}".format(self.add_name("typing.overload")))
638
639 if ctx.is_abstract:
640 decorators.append("@{}".format(self.add_name("abc.abstractmethod")))
641
642 if class_info is not None:
643 if self.is_staticmethod(class_info, name, obj):
644 decorators.append("@staticmethod")
645 else:
646 for sig in inferred:
647 if not sig.args or sig.args[0].name not in ("self", "cls"):
648 sig.args.insert(0, ArgSig(name=class_info.self_var))
649 # a sig generator indicates @classmethod by specifying the cls arg.
650 if inferred[0].args and inferred[0].args[0].name == "cls":
651 decorators.append("@classmethod")
652
653 docstring = self._indent_docstring(ctx.docstring) if ctx.docstring else None
654 output.extend(self.format_func_def(inferred, decorators=decorators, docstring=docstring))
655 self._fix_iter(ctx, inferred, output)
656
657 def _indent_docstring(self, docstring: str) -> str:
658 """Fix indentation of docstring extracted from pybind11 or other binding generators."""

Calls 15

is_abstract_methodMethod · 0.95
process_inferred_sigsMethod · 0.95
is_staticmethodMethod · 0.95
_indent_docstringMethod · 0.95
_fix_iterMethod · 0.95
FunctionContextClass · 0.90
ArgSigClass · 0.90
getattrFunction · 0.85
isinstanceFunction · 0.85
lenFunction · 0.85
is_private_nameMethod · 0.80