diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 29b2636d39cc..368023888b99 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -309,7 +309,7 @@ def get_annotation(key: str) -> str | None: if defaults and i >= len(args) - len(defaults): default_value = defaults[i - (len(args) - len(defaults))] if arg in annotations: - argtype = annotations[arg] + argtype = get_annotation(arg) else: argtype = self.get_type_annotation(default_value) if argtype == "None": @@ -735,7 +735,9 @@ def get_type_fullname(self, typ: type) -> str: typename = getattr(typ, "__qualname__", typ.__name__) module_name = self.get_obj_module(typ) assert module_name is not None, typ - if module_name != "builtins": + if module_name == "typing" and typename == "Optional": + typename = str(typ) + elif module_name != "builtins": typename = f"{module_name}.{typename}" return typename @@ -832,7 +834,14 @@ def generate_class_stub(self, class_name: str, cls: type, output: list[str]) -> bases_str = "(%s)" % ", ".join(bases) else: bases_str = "" - if types or static_properties or rw_properties or methods or ro_properties: + if ( + types + or static_properties + or rw_properties + or methods + or ro_properties + or class_info.docstring + ): output.append(f"{self._indent}class {class_name}{bases_str}:") for line in types: if ( @@ -843,6 +852,10 @@ def generate_class_stub(self, class_name: str, cls: type, output: list[str]) -> ): output.append("") output.append(line) + if class_info.docstring: + self.indent() + output.append(f'{self._indent}"""{class_info.docstring}"""') + self.dedent() for line in static_properties: output.append(line) for line in rw_properties: