Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import keyword
import os.path
from collections.abc import Callable, Mapping
from types import FunctionType, ModuleType
from types import FunctionType, ModuleType, UnionType
from typing import Any

from mypy.fastparse import parse_type_comment
Expand Down Expand Up @@ -768,10 +768,16 @@ def generate_property_stub(

rw_properties.append(f"{self._indent}{name}: {inferred_type}")

def get_type_fullname(self, typ: type) -> str:
def get_type_fullname(self, typ: type | UnionType) -> str:
"""Given a type, return a string representation"""
if typ is Any:
return "Any"
if isinstance(typ, UnionType):
# PEP 604 unions (``X | Y``) have no ``__name__`` / ``__qualname__``.
return " | ".join(
"None" if arg is type(None) else self.get_type_fullname(arg)
for arg in typ.__args__
)
typename = getattr(typ, "__qualname__", typ.__name__)
module_name = self.get_obj_module(typ)
if module_name is None:
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def f(x='foo'): ...
[out]
def f(x: str = ...): ...

[case testInspectUnionType_inspect]
def f(x: int | str) -> float | None: ...
[out]
def f(x: int | str) -> float | None: ...

[case testDefaultArgBytes]
def f(x=b'foo',y=b"what's up",z=b'\xc3\xa0 la une'): ...
[out]
Expand Down
Loading