18 bool check(PyObject *o) {
return PyFloat_Check(o) != 0; }
20 PyObject *
conv(PyObject *o) {
21 PyObject *
ret =
nullptr;
22 if (PyLong_Check(o)) {
23 double v = PyLong_AsDouble(o);
24 if (!(
v == -1.0 && PyErr_Occurred())) {
25 ret = PyFloat_FromDouble(
v);
40 double get_value()
const {
return PyFloat_AsDouble(this->ptr()); }
61 PyObject *ptr = Py_None;
65 PyObject *
const ptr = Py_None;
84 operator PyObject *() {
return Py_None; }
90 operator PyObject *()
const {
return Py_None; }
95 auto h = py::handle(obj);
96 return h.ptr() == Py_None;
101 auto h = py::handle(obj);
102 return h.ptr() == Py_None;
106 m.def(
"handle_from_move_only_type_with_operator_PyObject_ncnst",
from_ncnst);
107 m.def(
"handle_from_move_only_type_with_operator_PyObject_const",
from_const);
112 #if defined(__cpp_nontype_template_parameter_class)
114 enum Color { RED = 0, BLUE = 1 };
116 typedef py::typing::Literal<
"26",
127 typedef py::typing::TypeVar<
"T"> TypeVarT;
128 typedef py::typing::TypeVar<
"V"> TypeVarV;
138 m.def(
"get_bool", [] {
return py::bool_(
false); });
140 m.def(
"get_int", [] {
return py::int_(0); });
142 m.def(
"get_iterator", [] {
return py::iterator(); });
144 m.def(
"get_iterable", [] {
return py::iterable(); });
145 m.def(
"get_frozenset_from_iterable",
146 [](
const py::iterable &
iter) {
return py::frozenset(
iter); });
147 m.def(
"get_list_from_iterable", [](
const py::iterable &
iter) {
return py::list(
iter); });
148 m.def(
"get_set_from_iterable", [](
const py::iterable &
iter) {
return py::set(
iter); });
149 m.def(
"get_tuple_from_iterable", [](
const py::iterable &
iter) {
return py::tuple(
iter); });
151 m.def(
"get_float", [] {
return py::float_(0.0
f); });
153 m.def(
"list_no_args", []() {
return py::list{}; });
154 m.def(
"list_ssize_t", []() {
return py::list{(
py::ssize_t) 0}; });
155 m.def(
"list_size_t", []() {
return py::list{(
py::size_t) 0}; });
156 m.def(
"list_insert_ssize_t", [](py::list *
l) {
return l->insert((
py::ssize_t) 1, 83); });
157 m.def(
"list_insert_size_t", [](py::list *
l) {
return l->insert((
py::size_t) 3, 57); });
158 m.def(
"list_clear", [](py::list *
l) {
l->clear(); });
159 m.def(
"get_list", []() {
161 list.append(
"value");
162 py::print(
"Entry at position 0:", list[0]);
163 list[0] =
py::str(
"overwritten");
164 list.insert(0,
"inserted-0");
165 list.insert(2,
"inserted-2");
168 m.def(
"print_list", [](
const py::list &list) {
170 for (
auto item : list) {
171 py::print(
"list item {}: {}"_s.format(index++, item));
175 m.def(
"get_none", [] {
return py::none(); });
176 m.def(
"print_none", [](
const py::none &
none) {
py::print(
"none: {}"_s.format(
none)); });
179 m.def(
"get_set", []() {
183 set.add(std::string(
"key3"));
186 m.def(
"get_frozenset", []() {
190 set.add(std::string(
"key3"));
191 return py::frozenset(
set);
193 m.def(
"print_anyset", [](
const py::anyset &
set) {
194 for (
auto item :
set) {
198 m.def(
"anyset_size", [](
const py::anyset &
set) {
return set.size(); });
199 m.def(
"anyset_empty", [](
const py::anyset &
set) {
return set.empty(); });
200 m.def(
"anyset_contains",
201 [](
const py::anyset &
set,
const py::object &
key) {
return set.contains(
key); });
202 m.def(
"anyset_contains",
203 [](
const py::anyset &
set,
const char *
key) {
return set.contains(
key); });
208 m.def(
"get_dict", []() {
return py::dict(
"key"_a =
"value"); });
209 m.def(
"print_dict", [](
const py::dict &
dict) {
210 for (
auto item :
dict) {
211 py::print(
"key: {}, value={}"_s.format(item.first, item.second));
214 m.def(
"dict_keyword_constructor", []() {
215 auto d1 = py::dict(
"x"_a = 1,
"y"_a = 2);
216 auto d2 = py::dict(
"z"_a = 3, **d1);
219 m.def(
"dict_contains",
220 [](
const py::dict &
dict,
const py::object &val) {
return dict.
contains(val); });
221 m.def(
"dict_contains",
225 m.def(
"tuple_no_args", []() {
return py::tuple{}; });
226 m.def(
"tuple_ssize_t", []() {
return py::tuple{(
py::ssize_t) 0}; });
227 m.def(
"tuple_size_t", []() {
return py::tuple{(
py::size_t) 0}; });
228 m.def(
"get_tuple", []() {
return py::make_tuple(42, py::none(),
"spam"); });
231 m.def(
"get_simple_namespace", []() {
232 auto ns = py::module_::import(
"types").attr(
"SimpleNamespace")(
233 "attr"_a = 42,
"x"_a =
"foo",
"wrong"_a = 1);
242 m.def(
"str_from_string", []() {
return py::str(std::string(
"baz")); });
243 m.def(
"str_from_std_string_input", [](
const std::string &stri) {
return py::str(stri); });
245 m.def(
"str_from_bytes", []() {
return py::str(py::bytes(
"boo", 3)); });
246 m.def(
"str_from_bytes_input",
247 [](
const py::bytes &encoded_str) {
return py::str(encoded_str); });
249 m.def(
"str_from_object", [](
const py::object &obj) {
return py::str(obj); });
250 m.def(
"repr_from_object", [](
const py::object &obj) {
return py::repr(obj); });
251 m.def(
"str_from_handle", [](py::handle
h) {
return py::str(
h); });
252 m.def(
"str_from_string_from_str",
253 [](
const py::str &obj) {
return py::str(
static_cast<std::string
>(obj)); });
255 m.def(
"str_format", []() {
256 auto s1 =
"{} + {} = {}"_s.format(1, 2, 3);
257 auto s2 =
"{a} + {b} = {c}"_s.format(
"a"_a = 1,
"b"_a = 2,
"c"_a = 3);
262 m.def(
"bytes_from_char_ssize_t", []() {
return py::bytes{
"green", (
py::ssize_t) 5}; });
263 m.def(
"bytes_from_char_size_t", []() {
return py::bytes{
"purple", (
py::size_t) 6}; });
264 m.def(
"bytes_from_string", []() {
return py::bytes(std::string(
"foo")); });
265 m.def(
"bytes_from_str", []() {
return py::bytes(
py::str(
"bar", 3)); });
268 m.def(
"bytearray_from_char_ssize_t", []() {
return py::bytearray{
"$%", (
py::ssize_t) 2}; });
269 m.def(
"bytearray_from_char_size_t", []() {
return py::bytearray{
"@$!", (
py::size_t) 3}; });
270 m.def(
"bytearray_from_string", []() {
return py::bytearray(std::string(
"foo")); });
271 m.def(
"bytearray_size", []() {
return py::bytearray(
"foo").size(); });
274 m.def(
"return_capsule_with_destructor", []() {
276 return py::capsule([]() {
py::print(
"destructing capsule"); });
279 m.def(
"return_renamed_capsule_with_destructor", []() {
281 auto cap = py::capsule([]() {
py::print(
"destructing capsule"); });
282 static const char *capsule_name =
"test_name1";
284 cap.set_name(capsule_name);
288 m.def(
"return_capsule_with_destructor_2", []() {
290 return py::capsule((
void *) 1234, [](
void *ptr) {
291 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
295 m.def(
"return_capsule_with_destructor_3", []() {
297 auto cap = py::capsule((
void *) 1233,
"oname", [](
void *ptr) {
298 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
300 py::print(
"original name: {}"_s.format(cap.name()));
304 m.def(
"return_renamed_capsule_with_destructor_2", []() {
306 auto cap = py::capsule((
void *) 1234, [](
void *ptr) {
307 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
309 static const char *capsule_name =
"test_name2";
311 cap.set_name(capsule_name);
315 m.def(
"return_capsule_with_name_and_destructor", []() {
316 auto capsule = py::capsule((
void *) 12345,
"pointer type description", [](PyObject *ptr) {
318 const auto *
name = PyCapsule_GetName(ptr);
319 py::print(
"destructing capsule ({}, '{}')"_s.format(
320 (
size_t) PyCapsule_GetPointer(ptr,
name),
name));
324 capsule.set_pointer((
void *) 1234);
327 void *contents1 =
static_cast<void *
>(
capsule);
328 void *contents2 =
capsule.get_pointer();
329 void *contents3 =
capsule.get_pointer<
void>();
331 auto result1 =
reinterpret_cast<size_t>(contents1);
332 auto result2 =
reinterpret_cast<size_t>(contents2);
333 auto result3 =
reinterpret_cast<size_t>(contents3);
336 "created capsule ({}, '{}')"_s.format(result1 & result2 & result3,
capsule.name()));
340 m.def(
"return_capsule_with_explicit_nullptr_dtor", []() {
341 py::print(
"creating capsule with explicit nullptr dtor");
342 return py::capsule(
reinterpret_cast<void *
>(1234),
343 static_cast<void (*)(
void *)
>(
nullptr));
347 m.def(
"accessor_api", [](
const py::object &o) {
350 d[
"basic_attr"] = o.attr(
"basic_attr");
353 for (
auto item : o.attr(
"begin_end")) {
358 d[
"operator[object]"] = o.attr(
"d")[
"operator[object]"_s];
359 d[
"operator[char *]"] = o.attr(
"d")[
"operator[char *]"];
361 d[
"attr(object)"] = o.attr(
"sub").attr(
"attr_obj");
362 d[
"attr(char *)"] = o.attr(
"sub").attr(
"attr_char");
364 o.attr(
"sub").attr(
"missing").ptr();
365 }
catch (
const py::error_already_set &) {
366 d[
"missing_attr_ptr"] =
"raised"_s;
369 o.attr(
"missing").attr(
"doesn't matter");
370 }
catch (
const py::error_already_set &) {
371 d[
"missing_attr_chain"] =
"raised"_s;
374 d[
"is_none"] = o.attr(
"basic_attr").is_none();
376 d[
"operator()"] = o.attr(
"func")(1);
377 d[
"operator*"] = o.attr(
"func")(*o.attr(
"begin_end"));
380 py::list implicit_list = o.attr(
"begin_end");
381 d[
"implicit_list"] = implicit_list;
382 py::dict implicit_dict = o.attr(
"__dict__");
383 d[
"implicit_dict"] = implicit_dict;
388 m.def(
"tuple_accessor", [](
const py::tuple &existing_t) {
391 }
catch (
const py::error_already_set &) {
394 auto new_t = py::tuple(3);
395 for (
size_t i = 0;
i < new_t.size(); ++
i) {
403 m.def(
"accessor_assignment", []() {
404 auto l = py::list(1);
410 d[
"deferred_get"] = var;
414 d[
"deferred_set"] =
l[0];
420 m.def(
"accessor_moves", []() {
421 py::list return_list;
422 #ifdef PYBIND11_HANDLE_REF_DEBUG
423 py::int_ py_int_0(0);
424 py::int_ py_int_42(42);
429 py::sequence
seq(tup);
434 # define PYBIND11_LOCAL_DEF(...) \
436 std::size_t inc_refs = py::handle::inc_ref_counter(); \
438 inc_refs = py::handle::inc_ref_counter() - inc_refs; \
439 return_list.append(inc_refs); \
442 PYBIND11_LOCAL_DEF(tup[py_int_0])
443 PYBIND11_LOCAL_DEF(tup[py::int_(0)])
445 PYBIND11_LOCAL_DEF(tup.attr(py_str_count))
446 PYBIND11_LOCAL_DEF(tup.attr(
py::str(
"count")))
448 PYBIND11_LOCAL_DEF(
seq[py_int_0])
449 PYBIND11_LOCAL_DEF(
seq[py::int_(0)])
451 PYBIND11_LOCAL_DEF(
seq.attr(py_str_count))
452 PYBIND11_LOCAL_DEF(
seq.attr(
py::str(
"count")))
454 PYBIND11_LOCAL_DEF(lst[py_int_0])
455 PYBIND11_LOCAL_DEF(lst[py::int_(0)])
457 PYBIND11_LOCAL_DEF(lst.attr(py_str_count))
458 PYBIND11_LOCAL_DEF(lst.attr(
py::str(
"count")))
460 auto lst_acc = lst[py::int_(0)];
461 lst_acc = py::int_(42);
462 PYBIND11_LOCAL_DEF(lst_acc = py_int_42)
463 PYBIND11_LOCAL_DEF(lst_acc = py::int_(42))
464 # undef PYBIND11_LOCAL_DEF
470 m.def(
"default_constructors", []() {
471 return py::dict(
"bytes"_a = py::bytes(),
472 "bytearray"_a = py::bytearray(),
474 "bool"_a = py::bool_(),
475 "int"_a = py::int_(),
476 "float"_a = py::float_(),
477 "tuple"_a = py::tuple(),
478 "list"_a = py::list(),
479 "dict"_a = py::dict(),
483 m.def(
"converting_constructors", [](
const py::dict &
d) {
484 return py::dict(
"bytes"_a = py::bytes(
d[
"bytes"]),
485 "bytearray"_a = py::bytearray(
d[
"bytearray"]),
487 "bool"_a = py::bool_(
d[
"bool"]),
488 "int"_a = py::int_(
d[
"int"]),
489 "float"_a = py::float_(
d[
"float"]),
490 "tuple"_a = py::tuple(
d[
"tuple"]),
491 "list"_a = py::list(
d[
"list"]),
492 "dict"_a = py::dict(
d[
"dict"]),
494 "frozenset"_a = py::frozenset(
d[
"frozenset"]),
495 "memoryview"_a = py::memoryview(
d[
"memoryview"]));
498 m.def(
"cast_functions", [](
const py::dict &
d) {
500 return py::dict(
"bytes"_a =
d[
"bytes"].cast<py::bytes>(),
501 "bytearray"_a =
d[
"bytearray"].cast<py::bytearray>(),
502 "str"_a =
d[
"str"].cast<py::str>(),
503 "bool"_a =
d[
"bool"].cast<py::bool_>(),
504 "int"_a =
d[
"int"].cast<py::int_>(),
505 "float"_a =
d[
"float"].cast<py::float_>(),
506 "tuple"_a =
d[
"tuple"].cast<py::tuple>(),
507 "list"_a =
d[
"list"].cast<py::list>(),
508 "dict"_a =
d[
"dict"].cast<py::dict>(),
509 "set"_a =
d[
"set"].cast<py::set>(),
510 "frozenset"_a =
d[
"frozenset"].cast<py::frozenset>(),
511 "memoryview"_a =
d[
"memoryview"].cast<py::memoryview>());
514 m.def(
"convert_to_pybind11_str", [](
const py::object &o) {
return py::str(o); });
516 m.def(
"nonconverting_constructor",
517 [](
const std::string &
type, py::object
value,
bool move) -> py::object {
518 if (
type ==
"bytes") {
521 if (
type ==
"none") {
524 if (
type ==
"ellipsis") {
525 return move ? py::ellipsis(std::move(
value)) : py::ellipsis(
value);
527 if (
type ==
"type") {
530 throw std::runtime_error(
"Invalid type");
533 m.def(
"get_implicit_casting", []() {
535 d[
"char*_i1"] =
"abc";
536 const char *
c2 =
"abc";
547 d[
"int_p"] = py::int_(
i);
549 d[
"str_i1"] = std::string(
"str");
550 std::string s2(
"str1");
562 l.append(py::int_(15));
564 return py::dict(
"d"_a =
d,
"l"_a =
l);
568 m.def(
"print_function", []() {
570 py::print(1, 2.0,
"three",
true, std::string(
"-- multiple args"));
573 py::print(
"no new line here",
"end"_a =
" -- ");
576 auto py_stderr = py::module_::import(
"sys").attr(
"stderr");
577 py::print(
"this goes to stderr",
"file"_a = py_stderr);
582 "{a} + {b} = {c}"_s.format(
"a"_a =
"py::print",
"b"_a =
"str.format",
"c"_a =
"this"));
587 m.def(
"hash_function", [](py::object obj) {
return py::hash(std::move(obj)); });
589 m.def(
"obj_contains",
590 [](py::object &obj,
const py::object &
key) {
return obj.contains(
key); });
592 m.def(
"test_number_protocol", [](
const py::object &
a,
const py::object &
b) {
594 l.append(
a.equal(
b));
595 l.append(
a.not_equal(
b));
612 m.def(
"test_list_slicing", [](
const py::list &
a) {
return a[py::slice(0, -1, 2)]; });
615 m.def(
"issue2361_str_implicit_copy_none", []() {
616 py::str is_this_none = py::none();
619 m.def(
"issue2361_dict_implicit_copy_none", []() {
620 py::dict is_this_none = py::none();
624 m.def(
"test_memoryview_object", [](
const py::buffer &
b) {
return py::memoryview(
b); });
626 m.def(
"test_memoryview_buffer_info",
627 [](
const py::buffer &
b) {
return py::memoryview(
b.request()); });
629 m.def(
"test_memoryview_from_buffer", [](
bool is_unsigned) {
630 static const int16_t si16[] = {3, 1, 4, 1, 5};
631 static const uint16_t ui16[] = {2, 7, 1, 8};
633 return py::memoryview::from_buffer(ui16, {4}, {
sizeof(
uint16_t)});
635 return py::memoryview::from_buffer(si16, {5}, {
sizeof(
int16_t)});
638 m.def(
"test_memoryview_from_buffer_nativeformat", []() {
639 static const char *
format =
"@i";
644 m.def(
"test_memoryview_from_buffer_empty_shape", []() {
645 static const char *buf =
"";
646 return py::memoryview::from_buffer(buf, 1,
"B", {}, {});
649 m.def(
"test_memoryview_from_buffer_invalid_strides", []() {
650 static const char *buf =
"\x02\x03\x04";
651 return py::memoryview::from_buffer(buf, 1,
"B", {3}, {});
654 m.def(
"test_memoryview_from_buffer_nullptr", []() {
655 return py::memoryview::from_buffer(
static_cast<void *
>(
nullptr), 1,
"B", {}, {});
658 m.def(
"test_memoryview_from_memory", []() {
659 const char *buf =
"\xff\xe1\xab\x37";
660 return py::memoryview::from_memory(buf,
static_cast<py::ssize_t>(strlen(buf)));
664 m.def(
"get_len", [](py::handle
h) {
return py::len(
h); });
666 #ifdef PYBIND11_STR_LEGACY_PERMISSIVE
667 m.attr(
"PYBIND11_STR_LEGACY_PERMISSIVE") =
true;
670 m.def(
"isinstance_pybind11_bytes",
671 [](py::object o) {
return py::isinstance<py::bytes>(std::move(o)); });
672 m.def(
"isinstance_pybind11_str",
673 [](py::object o) {
return py::isinstance<py::str>(std::move(o)); });
675 m.def(
"pass_to_pybind11_bytes", [](py::bytes
b) {
return py::len(std::move(
b)); });
677 m.def(
"pass_to_std_string", [](
const std::string &
s) {
return s.size(); });
680 m.def(
"weakref_from_handle", [](py::handle
h) {
return py::weakref(
h); });
681 m.def(
"weakref_from_handle_and_function",
682 [](py::handle
h, py::function
f) {
return py::weakref(
h, std::move(
f)); });
683 m.def(
"weakref_from_object", [](
const py::object &o) {
return py::weakref(o); });
684 m.def(
"weakref_from_object_and_function",
685 [](py::object o, py::function
f) {
return py::weakref(std::move(o), std::move(
f)); });
691 #if (defined(__APPLE__) && defined(__clang__)) || defined(PYPY_VERSION)
693 # define PYBIND11_AUTO_IT auto it
697 # define PYBIND11_AUTO_IT auto &it
700 m.def(
"tuple_iterator", []() {
704 tup_sum += it.cast<
int>();
709 m.def(
"dict_iterator", []() {
711 dct[py::int_(3)] = 5;
712 dct[py::int_(7)] = 11;
715 kv_sum += it.first.cast<
int>() * 100 + it.second.cast<
int>();
720 m.def(
"passed_iterator", [](
const py::iterator &py_it) {
723 elem_sum += it.cast<
int>();
728 #undef PYBIND11_AUTO_IT
732 m.def(
"sequence_item_get_ssize_t", [](
const py::object &o) {
735 m.def(
"sequence_item_set_ssize_t", [](
const py::object &o) {
739 m.def(
"sequence_item_get_size_t", [](
const py::object &o) {
742 m.def(
"sequence_item_set_size_t", [](
const py::object &o) {
746 m.def(
"list_item_get_ssize_t", [](
const py::object &o) {
749 m.def(
"list_item_set_ssize_t", [](
const py::object &o) {
753 m.def(
"list_item_get_size_t", [](
const py::object &o) {
756 m.def(
"list_item_set_size_t", [](
const py::object &o) {
760 m.def(
"tuple_item_get_ssize_t", [](
const py::object &o) {
763 m.def(
"tuple_item_set_ssize_t", []() {
765 auto s1 =
py::str{
"edmond", 6};
766 auto o = py::tuple{2};
771 m.def(
"tuple_item_get_size_t", [](
const py::object &o) {
774 m.def(
"tuple_item_set_size_t", []() {
777 auto o = py::tuple{2};
784 double v =
x.get_value();
788 m.def(
"tuple_rvalue_getter", [](
const py::tuple &tup) {
790 for (
size_t i = 0;
i < tup.size();
i++) {
791 auto o = py::handle(tup[py::int_(
i)]);
793 throw py::value_error(
"tuple is malformed");
798 m.def(
"list_rvalue_getter", [](
const py::list &
l) {
800 for (
size_t i = 0;
i <
l.size();
i++) {
801 auto o = py::handle(
l[py::int_(
i)]);
803 throw py::value_error(
"list is malformed");
808 m.def(
"populate_dict_rvalue", [](
int population) {
810 for (
int i = 0;
i < population;
i++) {
811 d[py::int_(
i)] = py::int_(
i);
815 m.def(
"populate_obj_str_attrs", [](py::object &o,
int population) {
816 for (
int i = 0;
i < population;
i++) {
823 m.def(
"inplace_append", [](py::object &
a,
const py::object &
b) {
827 m.def(
"inplace_subtract", [](py::object &
a,
const py::object &
b) {
831 m.def(
"inplace_multiply", [](py::object &
a,
const py::object &
b) {
835 m.def(
"inplace_divide", [](py::object &
a,
const py::object &
b) {
839 m.def(
"inplace_or", [](py::object &
a,
const py::object &
b) {
843 m.def(
"inplace_and", [](py::object &
a,
const py::object &
b) {
847 m.def(
"inplace_lshift", [](py::object &
a,
const py::object &
b) {
851 m.def(
"inplace_rshift", [](py::object &
a,
const py::object &
b) {
856 m.def(
"annotate_tuple_float_str", [](
const py::typing::Tuple<py::float_, py::str> &) {});
857 m.def(
"annotate_tuple_empty", [](
const py::typing::Tuple<> &) {});
858 m.def(
"annotate_tuple_variable_length",
859 [](
const py::typing::Tuple<py::float_, py::ellipsis> &) {});
860 m.def(
"annotate_dict_str_int", [](
const py::typing::Dict<py::str, int> &) {});
861 m.def(
"annotate_list_int", [](
const py::typing::List<int> &) {});
862 m.def(
"annotate_set_str", [](
const py::typing::Set<std::string> &) {});
863 m.def(
"annotate_iterable_str", [](
const py::typing::Iterable<std::string> &) {});
864 m.def(
"annotate_iterator_int", [](
const py::typing::Iterator<int> &) {});
866 [](
const py::typing::Callable<
int(py::typing::List<py::str>,
py::str)> &) {});
868 m.def(
"annotate_fn_only_return", [](
const py::typing::Callable<
int(py::ellipsis)> &) {});
869 m.def(
"annotate_type", [](
const py::typing::Type<int> &
t) ->
py::type {
return t; });
871 m.def(
"annotate_union",
872 [](py::typing::List<py::typing::Union<py::str, py::int_, py::object>>
l,
875 py::object
c) -> py::typing::List<py::typing::Union<py::str, py::int_, py::object>> {
882 m.def(
"union_typing_only",
883 [](py::typing::List<py::typing::Union<py::str>> &
l)
884 -> py::typing::List<py::typing::Union<py::int_>> {
return l; });
886 m.def(
"annotate_union_to_object",
887 [](py::typing::Union<int, py::str> &o) -> py::object {
return o; });
889 m.def(
"annotate_optional",
890 [](py::list &list) -> py::typing::List<py::typing::Optional<py::str>> {
892 list.append(py::none());
896 m.def(
"annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard<py::str> {
897 return py::isinstance<py::str>(o);
899 m.def(
"annotate_type_is",
900 [](py::object &o) -> py::typing::TypeIs<py::str> {
return py::isinstance<py::str>(o); });
902 m.def(
"annotate_no_return", []() -> py::typing::NoReturn {
throw 0; });
903 m.def(
"annotate_never", []() -> py::typing::Never {
throw 0; });
905 m.def(
"annotate_optional_to_object",
906 [](py::typing::Optional<int> &o) -> py::object {
return o; });
908 #if defined(__cpp_nontype_template_parameter_class)
909 py::enum_<literals::Color>(
m,
"Color")
910 .value(
"RED", literals::Color::RED)
911 .value(
"BLUE", literals::Color::BLUE);
913 m.def(
"annotate_literal", [](literals::LiteralFoo &o) -> py::object {
return o; });
914 m.def(
"annotate_generic_containers",
915 [](
const py::typing::List<typevar::TypeVarT> &
l) -> py::typing::List<typevar::TypeVarV> {
919 m.def(
"annotate_listT_to_T",
920 [](
const py::typing::List<typevar::TypeVarT> &
l) -> typevar::TypeVarT {
return l[0]; });
921 m.def(
"annotate_object_to_T", [](
const py::object &o) -> typevar::TypeVarT {
return o; });
922 m.attr(
"if_defined__cpp_nontype_template_parameter_class") =
true;
924 m.attr(
"if_defined__cpp_nontype_template_parameter_class") =
false;