diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 7890b50b3b..1592b7b98c 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -241,7 +241,9 @@ struct type_caster::value && !is_std_char_t return PyLong_FromUnsignedLongLong((unsigned long long) src); } - PYBIND11_TYPE_CASTER(T, io_name::value>("typing.SupportsInt", "int", "typing.SupportsFloat", "float")); + PYBIND11_TYPE_CASTER(T, + io_name::value>( + "typing.SupportsInt", "int", "typing.SupportsFloat", "float")); }; template @@ -1345,8 +1347,8 @@ str_attr_accessor object_api::attr_with_type_hint(const char *key) const { if (ann.contains(key)) { throw std::runtime_error("__annotations__[\"" + std::string(key) + "\"] was set already."); } - - const char* text = make_caster::name.text; + + const char *text = make_caster::name.text; std::string signature; // `is_return_value.top()` is true if we are currently inside the return type of the @@ -1363,8 +1365,7 @@ str_attr_accessor object_api::attr_with_type_hint(const char *key) const { if (c == '!' && special_chars.find(*(pc + 1)) != std::string::npos) { // typing::Literal escapes special characters with ! signature += *++pc; - } - else if (c == '@') { + } else if (c == '@') { // `@^ ... @!` and `@$ ... @!` are used to force arg/return value type (see // typing::Callable/detail::arg_descr/detail::return_descr) if (*(pc + 1) == '^') { @@ -1407,8 +1408,7 @@ str_attr_accessor object_api::attr_with_type_hint(const char *key) const { signature += *pc++; } } - } - else{ + } else { signature += c; } } diff --git a/include/pybind11/detail/descr.h b/include/pybind11/detail/descr.h index 03c2917a49..42039a9c11 100644 --- a/include/pybind11/detail/descr.h +++ b/include/pybind11/detail/descr.h @@ -107,14 +107,14 @@ constexpr descr io_name(char const (&text1)[N1], char const (&text2 } template -constexpr enable_if_t> io_name(char const (&text1)[N1], char const (&text2)[N2], - char const (&)[N3], char const (&)[N4]) { +constexpr enable_if_t> +io_name(char const (&text1)[N1], char const (&text2)[N2], char const (&)[N3], char const (&)[N4]) { return io_name(text1, text2); } template -constexpr enable_if_t> io_name(char const (&)[N1], char const (&)[N2], - char const (&text3)[N3], char const (&text4)[N4]) { +constexpr enable_if_t> +io_name(char const (&)[N1], char const (&)[N2], char const (&text3)[N3], char const (&text4)[N4]) { return io_name(text3, text4); } diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 49d89e3ddf..09ff5d801c 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -228,7 +228,9 @@ struct handle_type_name> { template struct handle_type_name> { - static constexpr auto name = const_name("Final[") + ::pybind11::detail::return_descr(make_caster::name) + const_name("]"); + static constexpr auto name = const_name("Final[") + + ::pybind11::detail::return_descr(make_caster::name) + + const_name("]"); }; template diff --git a/tests/test_builtin_casters.cpp b/tests/test_builtin_casters.cpp index c57d60c8ae..c516f8de7e 100644 --- a/tests/test_builtin_casters.cpp +++ b/tests/test_builtin_casters.cpp @@ -240,7 +240,6 @@ TEST_SUBMODULE(builtin_casters, m) { m.def("float_passthrough", [](float arg) { return arg; }); m.def("float_passthrough_noconvert", [](float arg) { return arg; }, py::arg{}.noconvert()); - // test_tuple m.def( "pair_passthrough", diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 89e6838864..7a2c6a4d8f 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -322,7 +322,6 @@ def cant_convert(v): def test_float_convert(doc): - class Float: def __float__(self): return 41.45 diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 78812e28fd..32ce3d7b19 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -138,7 +138,10 @@ def test_cpp_function_roundtrip(): def test_function_signatures(doc): - assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[typing.SupportsInt], typing.SupportsInt]) -> str" + assert ( + doc(m.test_callback3) + == "test_callback3(arg0: Callable[[typing.SupportsInt], typing.SupportsInt]) -> str" + ) assert doc(m.test_callback4) == "test_callback4() -> Callable[[int], int]" diff --git a/tests/test_docstring_options.py b/tests/test_docstring_options.py index 3ad55321f6..f2a10480ca 100644 --- a/tests/test_docstring_options.py +++ b/tests/test_docstring_options.py @@ -19,9 +19,13 @@ def test_docstring_options(): assert m.test_overloaded3.__doc__ == "Overload docstr" # options.enable_function_signatures() - assert m.test_function3.__doc__.startswith("test_function3(a: typing.SupportsInt, b: typing.SupportsInt) -> None") + assert m.test_function3.__doc__.startswith( + "test_function3(a: typing.SupportsInt, b: typing.SupportsInt) -> None" + ) - assert m.test_function4.__doc__.startswith("test_function4(a: typing.SupportsInt, b: typing.SupportsInt) -> None") + assert m.test_function4.__doc__.startswith( + "test_function4(a: typing.SupportsInt, b: typing.SupportsInt) -> None" + ) assert m.test_function4.__doc__.endswith("A custom docstring\n") # options.disable_function_signatures() @@ -32,7 +36,9 @@ def test_docstring_options(): assert m.test_function6.__doc__ == "A custom docstring" # RAII destructor - assert m.test_function7.__doc__.startswith("test_function7(a: typing.SupportsInt, b: typing.SupportsInt) -> None") + assert m.test_function7.__doc__.startswith( + "test_function7(a: typing.SupportsInt, b: typing.SupportsInt) -> None" + ) assert m.test_function7.__doc__.endswith("A custom docstring\n") # when all options are disabled, no docstring (instead of an empty one) should be generated diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index c2adb0abda..a8e19f15bb 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -7,13 +7,31 @@ def test_function_signatures(doc): - assert doc(m.kw_func0) == "kw_func0(arg0: typing.SupportsInt, arg1: typing.SupportsInt) -> str" - assert doc(m.kw_func1) == "kw_func1(x: typing.SupportsInt, y: typing.SupportsInt) -> str" - assert doc(m.kw_func2) == "kw_func2(x: typing.SupportsInt = 100, y: typing.SupportsInt = 200) -> str" + assert ( + doc(m.kw_func0) + == "kw_func0(arg0: typing.SupportsInt, arg1: typing.SupportsInt) -> str" + ) + assert ( + doc(m.kw_func1) + == "kw_func1(x: typing.SupportsInt, y: typing.SupportsInt) -> str" + ) + assert ( + doc(m.kw_func2) + == "kw_func2(x: typing.SupportsInt = 100, y: typing.SupportsInt = 200) -> str" + ) assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None" - assert doc(m.kw_func4) == "kw_func4(myList: list[typing.SupportsInt] = [13, 17]) -> str" - assert doc(m.kw_func_udl) == "kw_func_udl(x: typing.SupportsInt, y: typing.SupportsInt = 300) -> str" - assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: typing.SupportsInt, y: typing.SupportsInt = 0) -> str" + assert ( + doc(m.kw_func4) + == "kw_func4(myList: list[typing.SupportsInt] = [13, 17]) -> str" + ) + assert ( + doc(m.kw_func_udl) + == "kw_func_udl(x: typing.SupportsInt, y: typing.SupportsInt = 300) -> str" + ) + assert ( + doc(m.kw_func_udl_z) + == "kw_func_udl_z(x: typing.SupportsInt, y: typing.SupportsInt = 0) -> str" + ) assert doc(m.args_function) == "args_function(*args) -> tuple" assert ( doc(m.args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple" @@ -374,10 +392,22 @@ def test_positional_only_args(): def test_signatures(): - assert m.kw_only_all.__doc__ == "kw_only_all(*, i: typing.SupportsInt, j: typing.SupportsInt) -> tuple\n" - assert m.kw_only_mixed.__doc__ == "kw_only_mixed(i: typing.SupportsInt, *, j: typing.SupportsInt) -> tuple\n" - assert m.pos_only_all.__doc__ == "pos_only_all(i: typing.SupportsInt, j: typing.SupportsInt, /) -> tuple\n" - assert m.pos_only_mix.__doc__ == "pos_only_mix(i: typing.SupportsInt, /, j: typing.SupportsInt) -> tuple\n" + assert ( + m.kw_only_all.__doc__ + == "kw_only_all(*, i: typing.SupportsInt, j: typing.SupportsInt) -> tuple\n" + ) + assert ( + m.kw_only_mixed.__doc__ + == "kw_only_mixed(i: typing.SupportsInt, *, j: typing.SupportsInt) -> tuple\n" + ) + assert ( + m.pos_only_all.__doc__ + == "pos_only_all(i: typing.SupportsInt, j: typing.SupportsInt, /) -> tuple\n" + ) + assert ( + m.pos_only_mix.__doc__ + == "pos_only_mix(i: typing.SupportsInt, /, j: typing.SupportsInt) -> tuple\n" + ) assert ( m.pos_kw_only_mix.__doc__ == "pos_kw_only_mix(i: typing.SupportsInt, /, j: typing.SupportsInt, *, k: typing.SupportsInt) -> tuple\n" diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 3b6c5e1449..43bbe9a233 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -521,10 +521,14 @@ def test_overload_ordering(): assert m.overload_order("string") == 1 assert m.overload_order(0) == 4 - assert "1. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__ + assert ( + "1. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__ + ) assert "2. overload_order(arg0: str) -> int" in m.overload_order.__doc__ assert "3. overload_order(arg0: str) -> int" in m.overload_order.__doc__ - assert "4. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__ + assert ( + "4. overload_order(arg0: typing.SupportsInt) -> int" in m.overload_order.__doc__ + ) with pytest.raises(TypeError) as err: m.overload_order(1.1) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index dad4a12bcb..deb4b06d41 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -942,7 +942,10 @@ def test_dict_annotations(doc): def test_list_annotations(doc): - assert doc(m.annotate_list_int) == "annotate_list_int(arg0: list[typing.SupportsInt]) -> None" + assert ( + doc(m.annotate_list_int) + == "annotate_list_int(arg0: list[typing.SupportsInt]) -> None" + ) def test_set_annotations(doc): @@ -978,7 +981,9 @@ def test_fn_return_only(doc): def test_type_annotation(doc): - assert doc(m.annotate_type) == "annotate_type(arg0: type[typing.SupportsInt]) -> type" + assert ( + doc(m.annotate_type) == "annotate_type(arg0: type[typing.SupportsInt]) -> type" + ) def test_union_annotations(doc): @@ -1182,7 +1187,9 @@ def test_class_attribute_types() -> None: assert empty_annotations is None assert static_annotations["x"] == "ClassVar[typing.SupportsFloat]" - assert static_annotations["dict_str_int"] == "ClassVar[dict[str, typing.SupportsInt]]" + assert ( + static_annotations["dict_str_int"] == "ClassVar[dict[str, typing.SupportsInt]]" + ) assert m.Static.x == 1.0 diff --git a/tests/test_stl.py b/tests/test_stl.py index 040092b5c9..29a6bf119f 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -64,7 +64,9 @@ def test_valarray(doc): assert m.load_valarray(tuple(lst)) assert doc(m.cast_valarray) == "cast_valarray() -> list[int]" - assert doc(m.load_valarray) == "load_valarray(arg0: list[typing.SupportsInt]) -> bool" + assert ( + doc(m.load_valarray) == "load_valarray(arg0: list[typing.SupportsInt]) -> bool" + ) def test_map(doc): @@ -325,7 +327,8 @@ def test_variant(doc): assert m.cast_variant() == (5, "Hello") assert ( - doc(m.load_variant) == "load_variant(arg0: Union[typing.SupportsInt, str, typing.SupportsFloat, None]) -> str" + doc(m.load_variant) + == "load_variant(arg0: Union[typing.SupportsInt, str, typing.SupportsFloat, None]) -> str" ) diff --git a/tests/test_type_caster_pyobject_ptr.py b/tests/test_type_caster_pyobject_ptr.py index 7c5768503c..5df8ca0196 100644 --- a/tests/test_type_caster_pyobject_ptr.py +++ b/tests/test_type_caster_pyobject_ptr.py @@ -102,7 +102,9 @@ def test_return_list_pyobject_ptr_reference(): def test_type_caster_name_via_incompatible_function_arguments_type_error(): - with pytest.raises(TypeError, match=r"1\. \(arg0: object, arg1: typing.SupportsInt\) -> None"): + with pytest.raises( + TypeError, match=r"1\. \(arg0: object, arg1: typing.SupportsInt\) -> None" + ): m.pass_pyobject_ptr_and_int(ValueHolder(101), ValueHolder(202))