From 179822f191483e802495545db4629a1a70d000ab Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 25 Jul 2026 12:48:31 -0400 Subject: [PATCH 1/2] fix: include `complex` in std::complex input annotation The input annotation for `std::complex` omitted the builtin `complex` type, which typeshed only makes a `typing.SupportsComplex` on Python 3.11+. Assisted-by: ClaudeCode:claude-opus-5 Claude-Session: https://claude.ai/code/session_013JABmnjt9oAh29p1pytAB3 --- include/pybind11/complex.h | 2 +- tests/test_builtin_casters.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h index 8f285d778f..5b84ed9bc9 100644 --- a/include/pybind11/complex.h +++ b/include/pybind11/complex.h @@ -88,7 +88,7 @@ class type_caster> { PYBIND11_TYPE_CASTER( std::complex, - io_name("typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex", + io_name("complex | typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex", "complex")); }; PYBIND11_NAMESPACE_END(detail) diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index b232c087e0..ee7333918c 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -565,7 +565,7 @@ def cant_convert(v): assert ( doc(convert) - == "complex_convert(arg0: typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex) -> complex" + == "complex_convert(arg0: complex | typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex) -> complex" ) assert doc(noconvert) == "complex_noconvert(arg0: complex) -> complex" From 87f6c85a60478b0087edb96672c9547e52ca4b3f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 26 Jul 2026 19:32:32 -0700 Subject: [PATCH 2/2] Add comment to explain why the explicit `complex` is needed. --- include/pybind11/complex.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h index 5b84ed9bc9..a6ec11a462 100644 --- a/include/pybind11/complex.h +++ b/include/pybind11/complex.h @@ -86,6 +86,8 @@ class type_caster> { return PyComplex_FromDoubles((double) src.real(), (double) src.imag()); } + // `complex` does not satisfy `typing.SupportsComplex` in typeshed for Python <= 3.10. + // Keep it explicit so generated stubs targeting those versions accept complex values. PYBIND11_TYPE_CASTER( std::complex, io_name("complex | typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex",