Skip to content

stubgen: handle PEP 604 unions in inspection mode (#21689)#21699

Open
apoorvdarshan wants to merge 1 commit into
python:masterfrom
apoorvdarshan:fix-21689-stubgen-pep604-union
Open

stubgen: handle PEP 604 unions in inspection mode (#21689)#21699
apoorvdarshan wants to merge 1 commit into
python:masterfrom
apoorvdarshan:fix-21689-stubgen-pep604-union

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Fixes #21689.

stubgen --inspect-mode crashes with AttributeError on any function whose signature uses PEP 604 union syntax (X | Y):

def process(value: int | str) -> float | None: ...
File ".../mypy/stubgenc.py", line 775, in get_type_fullname
    typename = getattr(typ, "__qualname__", typ.__name__)
AttributeError: 'types.UnionType' object has no attribute '__name__'

Root cause: InspectionStubGenerator.get_type_fullname() assumes the object is a class and reads __qualname__/__name__, but a runtime types.UnionType (the value of int | str) has neither.

Fix: render a UnionType by recursively formatting its members and joining with |, mapping NoneType to None so e.g. float | None renders correctly.

Added a _inspect test in test-data/unit/stubgen.test (fails on master, passes here). stubgen suite green (the one unrelated testAttrsClass_semanal failure is pre-existing in my environment), mypy self-check and ruff clean.

Disclosure: prepared with AI assistance; reviewed and verified locally.

InspectionStubGenerator.get_type_fullname() did
getattr(typ, '__qualname__', typ.__name__), which raised AttributeError
for a types.UnionType (PEP 604 'X | Y'), crashing 'stubgen --inspect-mode'
on any function whose signature uses the new union syntax.

Render a UnionType by joining its members, mapping NoneType to None.

Fixes python#21689.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant