Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of typing.Optional in stubgen #17197

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
):
Comment on lines -835 to +844
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this related to the issue with Optional? I think this change (and the one below) are causing the CI failure.

output.append(f"{self._indent}class {class_name}{bases_str}:")
for line in types:
if (
Expand All @@ -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:
Expand Down
Loading