diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h index 8f285d778f..a6ec11a462 100644 --- a/include/pybind11/complex.h +++ b/include/pybind11/complex.h @@ -86,9 +86,11 @@ 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("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"