From bb42eea7516c8a605967d9f6b830398cb3dfa065 Mon Sep 17 00:00:00 2001 From: Sergei Izmailov Date: Thu, 31 Aug 2023 08:14:25 +0900 Subject: [PATCH 1/2] fix: Render `py::function` as `Callable` --- include/pybind11/cast.h | 4 ++++ tests/test_callbacks.py | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index b3c8ebe17e..8a4e2e6476 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -898,6 +898,10 @@ struct handle_type_name { static constexpr auto name = const_name("float"); }; template <> +struct handle_type_name { + static constexpr auto name = const_name("Callable"); +}; +template <> struct handle_type_name { static constexpr auto name = const_name("None"); }; diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 4a652f53e8..a79975b238 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -83,7 +83,7 @@ def f(*args, **kwargs): "'expected_name' of type 'UnregisteredType' to Python object" if detailed_error_messages_enabled else "'expected_name' to Python object " - "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" + "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" ) @@ -104,20 +104,20 @@ def test_cpp_function_roundtrip(): """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer""" assert ( - m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2" + m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2" ) assert ( - m.test_dummy_function(m.roundtrip(m.dummy_function)) - == "matches dummy_function: eval(1) = 2" + m.test_dummy_function(m.roundtrip(m.dummy_function)) + == "matches dummy_function: eval(1) = 2" ) assert ( - m.test_dummy_function(m.dummy_function_overloaded) - == "matches dummy_function: eval(1) = 2" + m.test_dummy_function(m.dummy_function_overloaded) + == "matches dummy_function: eval(1) = 2" ) assert m.roundtrip(None, expect_none=True) is None assert ( - m.test_dummy_function(lambda x: x + 2) - == "can't convert to function pointer: eval(1) = 3" + m.test_dummy_function(lambda x: x + 2) + == "can't convert to function pointer: eval(1) = 3" ) with pytest.raises(TypeError) as excinfo: @@ -216,3 +216,7 @@ def test_custom_func(): def test_custom_func2(): assert m.custom_function2(3) == 27 assert m.roundtrip(m.custom_function2)(3) == 27 + + +def test_callback_docstring(): + assert m.test_tuple_unpacking.__doc__.strip() == "test_tuple_unpacking(arg0: Callable) -> object" From 3c39783272c2005fbeeffa437e2b1d32e69094a0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 31 Aug 2023 00:25:55 +0000 Subject: [PATCH 2/2] style: pre-commit fixes --- tests/test_callbacks.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index a79975b238..86c7674555 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -83,7 +83,7 @@ def f(*args, **kwargs): "'expected_name' of type 'UnregisteredType' to Python object" if detailed_error_messages_enabled else "'expected_name' to Python object " - "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" + "(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in debug mode for details)" ) @@ -104,20 +104,20 @@ def test_cpp_function_roundtrip(): """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer""" assert ( - m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2" + m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2" ) assert ( - m.test_dummy_function(m.roundtrip(m.dummy_function)) - == "matches dummy_function: eval(1) = 2" + m.test_dummy_function(m.roundtrip(m.dummy_function)) + == "matches dummy_function: eval(1) = 2" ) assert ( - m.test_dummy_function(m.dummy_function_overloaded) - == "matches dummy_function: eval(1) = 2" + m.test_dummy_function(m.dummy_function_overloaded) + == "matches dummy_function: eval(1) = 2" ) assert m.roundtrip(None, expect_none=True) is None assert ( - m.test_dummy_function(lambda x: x + 2) - == "can't convert to function pointer: eval(1) = 3" + m.test_dummy_function(lambda x: x + 2) + == "can't convert to function pointer: eval(1) = 3" ) with pytest.raises(TypeError) as excinfo: @@ -219,4 +219,7 @@ def test_custom_func2(): def test_callback_docstring(): - assert m.test_tuple_unpacking.__doc__.strip() == "test_tuple_unpacking(arg0: Callable) -> object" + assert ( + m.test_tuple_unpacking.__doc__.strip() + == "test_tuple_unpacking(arg0: Callable) -> object" + )