File tree Expand file tree Collapse file tree 3 files changed +23
-2
lines changed Expand file tree Collapse file tree 3 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -497,8 +497,10 @@ class cpp_function : public function {
497
497
} else if (c == ' @' ) {
498
498
// Handle types that differ depending on whether they appear
499
499
// in an argument or a return value position
500
+ // For named arguments (py::arg()) with noconvert set, use return value type
500
501
++pc;
501
- if (!is_return_value) {
502
+ if (!is_return_value
503
+ && !(arg_index < rec->args .size () && !rec->args [arg_index].convert )) {
502
504
while (*pc && *pc != ' @' )
503
505
signature += *pc++;
504
506
if (*pc == ' @' )
Original file line number Diff line number Diff line change @@ -161,7 +161,11 @@ struct type_caster<RealNumber> {
161
161
return py::float_ (number.value ).release ();
162
162
}
163
163
164
- bool load (handle src, bool ) {
164
+ bool load (handle src, bool convert) {
165
+ // If we're in no-convert mode, only load if given a float
166
+ if (!convert && !py::isinstance<py::float_>(src)) {
167
+ return false ;
168
+ }
165
169
if (!py::isinstance<py::float_>(src) && !py::isinstance<py::int_>(src)) {
166
170
return false ;
167
171
}
@@ -1067,6 +1071,14 @@ TEST_SUBMODULE(pytypes, m) {
1067
1071
m.attr (" defined___cpp_inline_variables" ) = false ;
1068
1072
#endif
1069
1073
m.def (" half_of_number" , [](const RealNumber &x) { return RealNumber{x.value / 2 }; });
1074
+ m.def (
1075
+ " half_of_number_convert" ,
1076
+ [](const RealNumber &x) { return RealNumber{x.value / 2 }; },
1077
+ py::arg (" x" ));
1078
+ m.def (
1079
+ " half_of_number_noconvert" ,
1080
+ [](const RealNumber &x) { return RealNumber{x.value / 2 }; },
1081
+ py::arg (" x" ).noconvert ());
1070
1082
// std::vector<T>
1071
1083
m.def (" half_of_number_vector" , [](const std::vector<RealNumber> &x) {
1072
1084
std::vector<RealNumber> result;
Original file line number Diff line number Diff line change @@ -1195,6 +1195,13 @@ def test_final_annotation() -> None:
1195
1195
1196
1196
def test_arg_return_type_hints (doc ):
1197
1197
assert doc (m .half_of_number ) == "half_of_number(arg0: Union[float, int]) -> float"
1198
+ assert (
1199
+ doc (m .half_of_number_convert )
1200
+ == "half_of_number_convert(x: Union[float, int]) -> float"
1201
+ )
1202
+ assert (
1203
+ doc (m .half_of_number_noconvert ) == "half_of_number_noconvert(x: float) -> float"
1204
+ )
1198
1205
assert m .half_of_number (2.0 ) == 1.0
1199
1206
assert m .half_of_number (2 ) == 1.0
1200
1207
assert m .half_of_number (0 ) == 0
You can’t perform that action at this time.
0 commit comments