@@ -318,3 +318,57 @@ like so:
318
318
319
319
py::class_<MyClass>("MyClass")
320
320
.def("myFunction", py::arg("arg") = (SomeType *) nullptr);
321
+
322
+ Non-converting arguments
323
+ ========================
324
+
325
+ Certain argument types may support conversion from one type to another. Some
326
+ examples of conversions are:
327
+
328
+ * :ref: `implicit_conversions ` declared using ``py::implicitly_convertible<A,B>() ``
329
+ * Calling a method accepting a double with an integer argument
330
+ * Calling a ``std::complex<float> `` argument with a non-complex python type
331
+ (for example, with a float). (Requires the optional ``pybind11/complex.h ``
332
+ header).
333
+ * Calling a function taking an Eigen matrix reference with a numpy array of the
334
+ wrong type or of an incompatible data layout. (Requires the optional
335
+ ``pybind11/eigen.h `` header).
336
+
337
+ This behaviour is sometimes undesirable: the binding code may prefer to raise
338
+ an error rather than convert the argument. This behaviour can be obtained
339
+ through ``py::arg `` by calling the ``.noconvert() `` method of the ``py::arg ``
340
+ object, such as:
341
+
342
+ .. code-block :: cpp
343
+
344
+ m.def("floats_only", [](double f) { return 0.5 * f; }, py::arg("f").noconvert());
345
+ m.def("floats_preferred", [](double f) { return 0.5 * f; }, py::arg("f"));
346
+
347
+ Attempting the call the second function (the one without ``.noconvert() ``) with
348
+ an integer will succeed, but attempting to call the ``.noconvert() `` version
349
+ will fail with a ``TypeError ``:
350
+
351
+ .. code-block :: pycon
352
+
353
+ >>> floats_preferred(4)
354
+ 2.0
355
+ >>> floats_only(4)
356
+ Traceback (most recent call last):
357
+ File "<stdin>", line 1, in <module>
358
+ TypeError: floats_only(): incompatible function arguments. The following argument types are supported:
359
+ 1. (f: float) -> float
360
+
361
+ Invoked with: 4
362
+
363
+ You may, of course, combine this with the :var: `_a ` shorthand notation (see
364
+ :ref: `keyword_args `) and/or :ref: `default_args `. It is also permitted to omit
365
+ the argument name by using the ``py::arg() `` constructor without an argument
366
+ name, i.e. by specifying ``py::arg().noconvert() ``.
367
+
368
+ .. note ::
369
+
370
+ When specifying ``py::arg `` options it is necessary to provide the same
371
+ number of options as the bound function has arguments. Thus if you want to
372
+ enable no-convert behaviour for just one of several arguments, you will
373
+ need to specify a ``py::arg() `` annotation for each argument with the
374
+ no-convert argument modified to ``py::arg().noconvert() ``.
0 commit comments