Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark function type caster as optional #910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Upcoming version (TBA)
long-standing inconvenience. (PR `#778
<https://github.com/wjakob/nanobind/pull/778>`__).

- The type caster for ``std::function`` now properly identifies its type as
optional (the runtime behavior is unaffected; this only impacts stubs)

* ABI version 16.


Expand Down
4 changes: 2 additions & 2 deletions include/nanobind/stl/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ struct type_caster<std::function<Return(Args...)>> {
std::conditional_t<std::is_void_v<Return>, void_type, Return>>;

NB_TYPE_CASTER(std::function <Return(Args...)>,
const_name(NB_TYPING_CALLABLE "[[") +
optional_name(const_name(NB_TYPING_CALLABLE "[[") +
concat(make_caster<Args>::Name...) + const_name("], ") +
ReturnCaster::Name + const_name("]"))
ReturnCaster::Name + const_name("]")))

struct pyfunc_wrapper_t : pyfunc_wrapper {
using pyfunc_wrapper::pyfunc_wrapper;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functions_ext.pyi.ref
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_bytearray_resize(arg0: bytearray, arg1: int, /) -> None: ...

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

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

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

Expand Down
12 changes: 6 additions & 6 deletions tests/test_stl_ext.pyi.ref
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class FuncWrapper:
def __init__(self) -> None: ...

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

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

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

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

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

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

Expand Down Expand Up @@ -182,15 +182,15 @@ def return_copyable() -> Copyable: ...

def return_copyable_ptr() -> Copyable: ...

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

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

def return_movable() -> Movable: ...

def return_movable_ptr() -> Movable: ...

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

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

Expand Down