Skip to content

Commit c7a8be2

Browse files
committed
Mark function type caster as optional
At runtime, the type caster will convert between Python `None` and an empty `std::function` object. However, the emitted stubs do not reflect this fact
1 parent 344e95e commit c7a8be2

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

docs/changelog.rst

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Upcoming version (TBA)
2525
long-standing inconvenience. (PR `#778
2626
<https://github.com/wjakob/nanobind/pull/778>`__).
2727

28+
- The type caster for ``std::function`` now properly identifies its type as
29+
optional (the runtime behavior is unaffected; this only impacts stubs)
30+
2831
* ABI version 16.
2932

3033

include/nanobind/stl/function.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ struct type_caster<std::function<Return(Args...)>> {
5050
std::conditional_t<std::is_void_v<Return>, void_type, Return>>;
5151

5252
NB_TYPE_CASTER(std::function <Return(Args...)>,
53-
const_name(NB_TYPING_CALLABLE "[[") +
53+
optional_name(const_name(NB_TYPING_CALLABLE "[[") +
5454
concat(make_caster<Args>::Name...) + const_name("], ") +
55-
ReturnCaster::Name + const_name("]"))
55+
ReturnCaster::Name + const_name("]")))
5656

5757
struct pyfunc_wrapper_t : pyfunc_wrapper {
5858
using pyfunc_wrapper::pyfunc_wrapper;

tests/test_functions_ext.pyi.ref

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def test_bytearray_resize(arg0: bytearray, arg1: int, /) -> None: ...
182182

183183
def test_bytearray_size(arg: bytearray, /) -> int: ...
184184

185-
def test_call_1(arg: Callable[[int], int], /) -> object: ...
185+
def test_call_1(arg: Callable[[int], int] | None, /) -> object: ...
186186

187187
def test_call_2(arg: Callable[[int, int], None], /) -> object: ...
188188

tests/test_stl_ext.pyi.ref

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class FuncWrapper:
3838
def __init__(self) -> None: ...
3939

4040
@property
41-
def f(self) -> Callable[[], None]: ...
41+
def f(self) -> Callable[[], None] | None: ...
4242

4343
@f.setter
44-
def f(self, arg: Callable[[], None], /) -> None: ...
44+
def f(self, arg: Callable[[], None] | None, /) -> None: ...
4545

4646
alive: int = ...
4747
"""static read-only property"""
@@ -87,7 +87,7 @@ def array_in(arg: Sequence[int], /) -> int: ...
8787

8888
def array_out() -> list[int]: ...
8989

90-
def call_function(arg0: Callable[[int], int], arg1: int, /) -> int: ...
90+
def call_function(arg0: Callable[[int], int] | None, arg1: int, /) -> int: ...
9191

9292
def complex_array_double(arg: Sequence[complex], /) -> list[complex]: ...
9393

@@ -182,15 +182,15 @@ def return_copyable() -> Copyable: ...
182182

183183
def return_copyable_ptr() -> Copyable: ...
184184

185-
def return_empty_function() -> Callable[[int], int]: ...
185+
def return_empty_function() -> Callable[[int], int] | None: ...
186186

187-
def return_function() -> Callable[[int], int]: ...
187+
def return_function() -> Callable[[int], int] | None: ...
188188

189189
def return_movable() -> Movable: ...
190190

191191
def return_movable_ptr() -> Movable: ...
192192

193-
def return_void_function(arg: Callable[[], None], /) -> Callable[[], None]: ...
193+
def return_void_function(arg: Callable[[], None] | None, /) -> Callable[[], None] | None: ...
194194

195195
def set_in_lvalue_ref(x: Set[str]) -> None: ...
196196

0 commit comments

Comments
 (0)