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

Method generate_class_stub

mypy/stubgenc.py:804–908  ·  view source on GitHub ↗

Generate stub for a single class using runtime introspection. The result lines will be appended to 'output'. If necessary, any required names will be added to 'imports'.

(
        self, class_name: str, cls: type, output: list[str], parent_class: ClassInfo | None = None
    )

Source from the content-addressed store, hash-verified

802 return [self.strip_or_import(self.get_type_fullname(base)) for base in bases]
803
804 def generate_class_stub(
805 self, class_name: str, cls: type, output: list[str], parent_class: ClassInfo | None = None
806 ) -> None:
807 """Generate stub for a single class using runtime introspection.
808
809 The result lines will be appended to 'output'. If necessary, any
810 required names will be added to 'imports'.
811 """
812 raw_lookup: Mapping[str, Any] = getattr(cls, "__dict__") # noqa: B009
813 items = self.get_members(cls)
814 if self.resort_members:
815 items = sorted(items, key=lambda x: method_name_sort_key(x[0]))
816 names = {x[0] for x in items}
817 methods: list[str] = []
818 types: list[str] = []
819 static_properties: list[str] = []
820 rw_properties: list[str] = []
821 ro_properties: list[str] = []
822 attrs: list[tuple[str, Any]] = []
823
824 self.record_name(class_name)
825 self.indent()
826
827 class_info = ClassInfo(
828 class_name, "", getattr(cls, "__doc__", None), cls, parent=parent_class
829 )
830
831 for attr, value in items:
832 # use unevaluated descriptors when dealing with property inspection
833 raw_value = raw_lookup.get(attr, value)
834 if self.is_method(class_info, attr, value) or self.is_classmethod(
835 class_info, attr, value
836 ):
837 if attr == "__new__":
838 # TODO: We should support __new__.
839 if "__init__" in names:
840 # Avoid duplicate functions if both are present.
841 # But is there any case where .__new__() has a
842 # better signature than __init__() ?
843 continue
844 attr = "__init__"
845 # FIXME: make this nicer
846 if self.is_staticmethod(class_info, attr, value):
847 class_info.self_var = ""
848 elif self.is_classmethod(class_info, attr, value):
849 class_info.self_var = "cls"
850 else:
851 class_info.self_var = "self"
852 self.generate_function_stub(attr, value, output=methods, class_info=class_info)
853 elif self.is_property(class_info, attr, raw_value):
854 self.generate_property_stub(
855 attr,
856 raw_value,
857 value,
858 static_properties,
859 rw_properties,
860 ro_properties,
861 class_info,

Calls 15

get_membersMethod · 0.95
is_methodMethod · 0.95
is_classmethodMethod · 0.95
is_staticmethodMethod · 0.95
is_propertyMethod · 0.95
is_defined_in_moduleMethod · 0.95
strip_or_importMethod · 0.95
get_type_annotationMethod · 0.95
get_base_typesMethod · 0.95
_indent_docstringMethod · 0.95