Skip to content

Handle PEP 604 unions in stubgen inspect mode#21775

Open
tarun-bandi wants to merge 1 commit into
python:masterfrom
tarun-bandi:agent/stubgen-pep604-union-inspect
Open

Handle PEP 604 unions in stubgen inspect mode#21775
tarun-bandi wants to merge 1 commit into
python:masterfrom
tarun-bandi:agent/stubgen-pep604-union-inspect

Conversation

@tarun-bandi

Copy link
Copy Markdown

Summary

Fixes stubgen --inspect-mode crashing when runtime annotations contain PEP 604 unions such as int | str.

Runtime inspection sees those annotations as types.UnionType, which does not define __name__. InspectionStubGenerator.get_type_fullname() now handles runtime union objects recursively and renders type(None) as None, so signatures like float | None stay valid in generated stubs.

Fixes #21689.

Test Plan

  • python -m pytest mypy/test/teststubgen.py::StubgencSuite::test_non_c_generate_signature_with_pep604_union -q
  • python -m pytest mypy/test/teststubgen.py::StubgencSuite -q
  • python -m py_compile mypy/stubgenc.py mypy/test/teststubgen.py
  • python -m black --fast --check mypy/stubgenc.py mypy/test/teststubgen.py
  • python -m ruff check mypy/stubgenc.py mypy/test/teststubgen.py
  • git diff --check

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stubgen --inspect-mode crashes with AttributeError on PEP 604 union syntax (X | Y) *(mypy 2.1.0)*

1 participant