Skip to content

Commit e76c308

Browse files
committed
Render NaN default argument as numpy.nan
Example: generated signature changes from foo(x: float = nan) to foo(x: float = numpy.nan)
1 parent eeb1044 commit e76c308

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

include/pybind11/cast.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,16 @@ struct arg_v : arg {
18711871
arg_v(const arg &base, T &&x, const char *descr = nullptr)
18721872
: arg_v(arg(base), std::forward<T>(x), descr) { }
18731873

1874+
#define ARGV_NAN_DEFAULT_OVERLOAD(FLOAT_TYPE) \
1875+
arg_v(const arg &base, FLOAT_TYPE x) \
1876+
: arg_v(arg(base), x, std::isnan(x) ? "numpy.nan" : nullptr) {}
1877+
1878+
ARGV_NAN_DEFAULT_OVERLOAD(float)
1879+
ARGV_NAN_DEFAULT_OVERLOAD(double)
1880+
ARGV_NAN_DEFAULT_OVERLOAD(long double)
1881+
1882+
#undef ARGV_NAN_DEFAULT_OVERLOAD
1883+
18741884
/// Same as `arg::noconvert()`, but returns *this as arg_v&, not arg&
18751885
arg_v &noconvert(bool flag = true) { arg::noconvert(flag); return *this; }
18761886

tests/test_kwargs_and_defaults.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
TEST_SUBMODULE(kwargs_and_defaults, m) {
1515
auto kw_func = [](int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); };
16+
auto kw_func_float = [](float x, double y, long double z) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y) + ", y=" + std::to_string(z); };
1617

1718
// test_named_arguments
1819
m.def("kw_func0", kw_func);
@@ -33,6 +34,13 @@ TEST_SUBMODULE(kwargs_and_defaults, m) {
3334
m.def("kw_func_udl", kw_func, "x"_a, "y"_a=300);
3435
m.def("kw_func_udl_z", kw_func, "x"_a, "y"_a=0);
3536

37+
m.def("kw_func_float_123", kw_func_float, "x"_a=1, "y"_a=2, "z"_a=3);
38+
m.def("kw_func_float_nan", kw_func_float,
39+
"x"_a=std::numeric_limits<float>::quiet_NaN(),
40+
"y"_a=std::numeric_limits<double>::quiet_NaN(),
41+
"z"_a=std::numeric_limits<long double>::quiet_NaN());
42+
43+
3644
// test_args_and_kwargs
3745
m.def("args_function", [](py::args args) -> py::tuple {
3846
return std::move(args);

tests/test_kwargs_and_defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def test_function_signatures(doc):
1010
assert doc(m.kw_func4) == "kw_func4(myList: List[int] = [13, 17]) -> str"
1111
assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int = 300) -> str"
1212
assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int = 0) -> str"
13+
assert doc(m.kw_func_float_123) == "kw_func_float_123(x: float = 1, y: float = 2, z: float = 3) -> str"
14+
assert doc(m.kw_func_float_nan) == "kw_func_float_nan(x: float = numpy.nan, y: float = numpy.nan, z: float = numpy.nan) -> str"
1315
assert doc(m.args_function) == "args_function(*args) -> tuple"
1416
assert doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
1517
assert doc(m.KWClass.foo0) == \

0 commit comments

Comments
 (0)