Skip to content
Merged
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
20 changes: 18 additions & 2 deletions mypy/stubgenc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import inspect
import keyword
import os.path
import types
from collections.abc import Callable, Mapping
from types import FunctionType, ModuleType
from typing import Any
Expand Down Expand Up @@ -768,11 +769,26 @@ 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: object) -> str:
"""Given a type, return a string representation"""
if typ is Any:
return "Any"
typename = getattr(typ, "__qualname__", typ.__name__)
if typ is type(None):
return "None"
# PEP 604 unions (X | Y) are instances of types.UnionType at runtime.
# They have neither __qualname__ nor __name__, so format them explicitly.
if isinstance(typ, types.UnionType):
return " | ".join(self.get_type_fullname(arg) for arg in typ.__args__)
# Avoid evaluating typ.__name__ as a getattr default: that is evaluated
# eagerly and crashes for types.UnionType and similar constructs.
typename = getattr(typ, "__qualname__", None)
if typename is None:
typename = getattr(typ, "__name__", None)
if typename is None:
# This should not normally happen, but some types may resist our
# introspection attempts too hard. See
# https://github.com/python/mypy/issues/19031
return "_typeshed.Incomplete"
module_name = self.get_obj_module(typ)
if module_name is None:
# This should not normally happen, but some types may resist our
Expand Down
22 changes: 22 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,28 @@ def __init__(self, arg0: str) -> None:
)
assert_equal(gen.get_imports().splitlines(), ["from typing import overload"])

def test_get_type_fullname_pep604_union(self) -> None:
# Regression test for https://github.com/python/mypy/issues/21689:
# types.UnionType (X | Y) must not crash under --inspect-mode.
mod = ModuleType("module", "")
gen = InspectionStubGenerator(mod.__name__, known_modules=[mod.__name__], module=mod)
assert_equal(gen.get_type_fullname(int | str), "int | str")
assert_equal(gen.get_type_fullname(float | None), "float | None")
assert_equal(gen.get_type_fullname(int | str | bytes), "int | str | bytes")

def test_generate_function_stub_pep604_union_annotations(self) -> None:
def process(value: int | str) -> float | None:
if isinstance(value, int):
return float(value)
return None

output: list[str] = []
mod = ModuleType(process.__module__, "")
gen = InspectionStubGenerator(mod.__name__, known_modules=[mod.__name__], module=mod)
gen.is_c_module = False
gen.generate_function_stub("process", process, output=output)
assert_equal(output, ["def process(value: int | str) -> float | None: ..."])


class ArgSigSuite(unittest.TestCase):
def test_repr(self) -> None:
Expand Down