From 91027de5530d141a288ba0237abb8bca821c3b8d Mon Sep 17 00:00:00 2001 From: Wesley Banfield Date: Tue, 30 Apr 2024 13:07:59 +0200 Subject: [PATCH 1/3] Fix handling of typing.Optional --- mypy/stubgenc.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 29b2636d39cc..57fe2ca03d40 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 From eb36c4030b2a1bab451b081eb3e497e21184bbd2 Mon Sep 17 00:00:00 2001 From: Wesley Banfield Date: Thu, 2 May 2024 11:06:08 +0200 Subject: [PATCH 2/3] Add class documentation to class docstrings --- mypy/stubgenc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 57fe2ca03d40..f7849c930925 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -834,7 +834,7 @@ 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 ( @@ -845,6 +845,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: From 9f2de41fdab5ec9da094fd6f319327dc36988bbe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 May 2024 09:06:35 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/stubgenc.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index f7849c930925..368023888b99 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -834,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 or class_info.docstring: + 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 (