fix: include `complex` in std::complex input annotation (#6113)
* fix: include `complex` in std::complex input annotation
The input annotation for `std::complex<T>` 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
* Add comment to explain why the explicit `complex` is needed.
---------
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
diff --git a/include/pybind11/complex.h b/include/pybind11/complex.h
index 8f285d7..a6ec11a 100644
--- a/include/pybind11/complex.h
+++ b/include/pybind11/complex.h
@@ -86,9 +86,11 @@
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<T>,
- 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 b232c08..ee73339 100644
--- a/tests/test_builtin_casters.py
+++ b/tests/test_builtin_casters.py
@@ -565,7 +565,7 @@
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"