Skip to content

Commit

Permalink
style: pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Feb 21, 2025
1 parent 13dd537 commit bfdee07
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 36 deletions.
14 changes: 7 additions & 7 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
return PyLong_FromUnsignedLongLong((unsigned long long) src);
}

PYBIND11_TYPE_CASTER(T, io_name<std::is_integral<T>::value>("typing.SupportsInt", "int", "typing.SupportsFloat", "float"));
PYBIND11_TYPE_CASTER(T,
io_name<std::is_integral<T>::value>(
"typing.SupportsInt", "int", "typing.SupportsFloat", "float"));
};

template <typename T>
Expand Down Expand Up @@ -1345,8 +1347,8 @@ str_attr_accessor object_api<D>::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<T>::name.text;

const char *text = make_caster<T>::name.text;

std::string signature;
// `is_return_value.top()` is true if we are currently inside the return type of the
Expand All @@ -1363,8 +1365,7 @@ str_attr_accessor object_api<D>::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) == '^') {
Expand Down Expand Up @@ -1407,8 +1408,7 @@ str_attr_accessor object_api<D>::attr_with_type_hint(const char *key) const {
signature += *pc++;
}
}
}
else{
} else {
signature += c;
}
}
Expand Down
8 changes: 4 additions & 4 deletions include/pybind11/detail/descr.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ constexpr descr<N1 + N2 + 1> io_name(char const (&text1)[N1], char const (&text2
}

template <bool B, size_t N1, size_t N2, size_t N3, size_t N4>
constexpr enable_if_t<B, descr<N1 + N2 + 1>> io_name(char const (&text1)[N1], char const (&text2)[N2],
char const (&)[N3], char const (&)[N4]) {
constexpr enable_if_t<B, descr<N1 + N2 + 1>>
io_name(char const (&text1)[N1], char const (&text2)[N2], char const (&)[N3], char const (&)[N4]) {
return io_name(text1, text2);
}

template <bool B, size_t N1, size_t N2, size_t N3, size_t N4>
constexpr enable_if_t<!B, descr<N3 + N4 + 1>> io_name(char const (&)[N1], char const (&)[N2],
char const (&text3)[N3], char const (&text4)[N4]) {
constexpr enable_if_t<!B, descr<N3 + N4 + 1>>
io_name(char const (&)[N1], char const (&)[N2], char const (&text3)[N3], char const (&text4)[N4]) {
return io_name(text3, text4);
}

Expand Down
4 changes: 3 additions & 1 deletion include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ struct handle_type_name<typing::Optional<T>> {

template <typename T>
struct handle_type_name<typing::Final<T>> {
static constexpr auto name = const_name("Final[") + ::pybind11::detail::return_descr(make_caster<T>::name) + const_name("]");
static constexpr auto name = const_name("Final[")
+ ::pybind11::detail::return_descr(make_caster<T>::name)
+ const_name("]");
};

template <typename T>
Expand Down
1 change: 0 additions & 1 deletion tests/test_builtin_casters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion tests/test_builtin_casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ def cant_convert(v):


def test_float_convert(doc):

class Float:
def __float__(self):
return 41.45
Expand Down
5 changes: 4 additions & 1 deletion tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"


Expand Down
12 changes: 9 additions & 3 deletions tests/test_docstring_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
50 changes: 40 additions & 10 deletions tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions tests/test_methods_and_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"
)


Expand Down
4 changes: 3 additions & 1 deletion tests/test_type_caster_pyobject_ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down

0 comments on commit bfdee07

Please sign in to comment.