17 #if defined(PYBIND11_CPP20) && __has_include(<ranges>)
18 # if !defined(PYBIND11_COMPILER_CLANG) || __clang_major__ >= 16 // llvm/llvm-project#52696
19 # define PYBIND11_TEST_PYTYPES_HAS_RANGES
26 bool check(PyObject *o) {
return PyFloat_Check(o) != 0; }
28 PyObject *
conv(PyObject *o) {
29 PyObject *
ret =
nullptr;
30 if (PyLong_Check(o)) {
31 double v = PyLong_AsDouble(o);
32 if (!(
v == -1.0 && PyErr_Occurred())) {
33 ret = PyFloat_FromDouble(
v);
48 double get_value()
const {
return PyFloat_AsDouble(this->ptr()); }
69 PyObject *ptr = Py_None;
73 PyObject *
const ptr = Py_None;
92 operator PyObject *() {
return Py_None; }
98 operator PyObject *()
const {
return Py_None; }
103 auto h = py::handle(obj);
104 return h.ptr() == Py_None;
109 auto h = py::handle(obj);
110 return h.ptr() == Py_None;
114 m.def(
"handle_from_move_only_type_with_operator_PyObject_ncnst",
from_ncnst);
115 m.def(
"handle_from_move_only_type_with_operator_PyObject_const",
from_const);
120 #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
122 enum Color { RED = 0, BLUE = 1 };
124 typedef py::typing::Literal<
"26",
135 typedef py::typing::TypeVar<
"T"> TypeVarT;
136 typedef py::typing::TypeVar<
"V"> TypeVarV;
146 m.def(
"get_bool", [] {
return py::bool_(
false); });
148 m.def(
"get_int", [] {
return py::int_(0); });
150 m.def(
"get_iterator", [] {
return py::iterator(); });
152 m.def(
"get_iterable", [] {
return py::iterable(); });
153 m.def(
"get_frozenset_from_iterable",
154 [](
const py::iterable &
iter) {
return py::frozenset(
iter); });
155 m.def(
"get_list_from_iterable", [](
const py::iterable &
iter) {
return py::list(
iter); });
156 m.def(
"get_set_from_iterable", [](
const py::iterable &
iter) {
return py::set(
iter); });
157 m.def(
"get_tuple_from_iterable", [](
const py::iterable &
iter) {
return py::tuple(
iter); });
159 m.def(
"get_float", [] {
return py::float_(0.0
f); });
161 m.def(
"list_no_args", []() {
return py::list{}; });
162 m.def(
"list_ssize_t", []() {
return py::list{(
py::ssize_t) 0}; });
163 m.def(
"list_size_t", []() {
return py::list{(
py::size_t) 0}; });
164 m.def(
"list_insert_ssize_t", [](py::list *
l) {
return l->insert((
py::ssize_t) 1, 83); });
165 m.def(
"list_insert_size_t", [](py::list *
l) {
return l->insert((
py::size_t) 3, 57); });
166 m.def(
"list_clear", [](py::list *
l) {
l->clear(); });
167 m.def(
"get_list", []() {
169 list.append(
"value");
170 py::print(
"Entry at position 0:", list[0]);
171 list[0] =
py::str(
"overwritten");
172 list.insert(0,
"inserted-0");
173 list.insert(2,
"inserted-2");
176 m.def(
"print_list", [](
const py::list &list) {
178 for (
auto item : list) {
179 py::print(
"list item {}: {}"_s.format(index++, item));
183 m.def(
"get_none", [] {
return py::none(); });
184 m.def(
"print_none", [](
const py::none &
none) {
py::print(
"none: {}"_s.format(
none)); });
187 m.def(
"get_set", []() {
191 set.add(std::string(
"key3"));
194 m.def(
"get_frozenset", []() {
198 set.add(std::string(
"key3"));
199 return py::frozenset(
set);
201 m.def(
"print_anyset", [](
const py::anyset &
set) {
202 for (
auto item :
set) {
206 m.def(
"anyset_size", [](
const py::anyset &
set) {
return set.size(); });
207 m.def(
"anyset_empty", [](
const py::anyset &
set) {
return set.empty(); });
208 m.def(
"anyset_contains",
209 [](
const py::anyset &
set,
const py::object &
key) {
return set.contains(
key); });
210 m.def(
"anyset_contains",
211 [](
const py::anyset &
set,
const char *
key) {
return set.contains(
key); });
216 m.def(
"get_dict", []() {
return py::dict(
"key"_a =
"value"); });
217 m.def(
"print_dict", [](
const py::dict &
dict) {
218 for (
auto item :
dict) {
219 py::print(
"key: {}, value={}"_s.format(item.first, item.second));
222 m.def(
"dict_keyword_constructor", []() {
223 auto d1 = py::dict(
"x"_a = 1,
"y"_a = 2);
224 auto d2 = py::dict(
"z"_a = 3, **d1);
227 m.def(
"dict_contains",
228 [](
const py::dict &
dict,
const py::object &val) {
return dict.
contains(val); });
229 m.def(
"dict_contains",
233 m.def(
"tuple_no_args", []() {
return py::tuple{}; });
234 m.def(
"tuple_ssize_t", []() {
return py::tuple{(
py::ssize_t) 0}; });
235 m.def(
"tuple_size_t", []() {
return py::tuple{(
py::size_t) 0}; });
236 m.def(
"get_tuple", []() {
return py::make_tuple(42, py::none(),
"spam"); });
239 m.def(
"get_simple_namespace", []() {
240 auto ns = py::module_::import(
"types").attr(
"SimpleNamespace")(
241 "attr"_a = 42,
"x"_a =
"foo",
"wrong"_a = 1);
250 m.def(
"str_from_string", []() {
return py::str(std::string(
"baz")); });
251 m.def(
"str_from_std_string_input", [](
const std::string &stri) {
return py::str(stri); });
253 m.def(
"str_from_bytes", []() {
return py::str(py::bytes(
"boo", 3)); });
254 m.def(
"str_from_bytes_input",
255 [](
const py::bytes &encoded_str) {
return py::str(encoded_str); });
257 m.def(
"str_from_object", [](
const py::object &obj) {
return py::str(obj); });
258 m.def(
"repr_from_object", [](
const py::object &obj) {
return py::repr(obj); });
259 m.def(
"str_from_handle", [](py::handle
h) {
return py::str(
h); });
260 m.def(
"str_from_string_from_str",
261 [](
const py::str &obj) {
return py::str(
static_cast<std::string
>(obj)); });
263 m.def(
"str_format", []() {
264 auto s1 =
"{} + {} = {}"_s.format(1, 2, 3);
265 auto s2 =
"{a} + {b} = {c}"_s.format(
"a"_a = 1,
"b"_a = 2,
"c"_a = 3);
270 m.def(
"bytes_from_char_ssize_t", []() {
return py::bytes{
"green", (
py::ssize_t) 5}; });
271 m.def(
"bytes_from_char_size_t", []() {
return py::bytes{
"purple", (
py::size_t) 6}; });
272 m.def(
"bytes_from_string", []() {
return py::bytes(std::string(
"foo")); });
273 m.def(
"bytes_from_str", []() {
return py::bytes(
py::str(
"bar", 3)); });
276 m.def(
"bytearray_from_char_ssize_t", []() {
return py::bytearray{
"$%", (
py::ssize_t) 2}; });
277 m.def(
"bytearray_from_char_size_t", []() {
return py::bytearray{
"@$!", (
py::size_t) 3}; });
278 m.def(
"bytearray_from_string", []() {
return py::bytearray(std::string(
"foo")); });
279 m.def(
"bytearray_size", []() {
return py::bytearray(
"foo").size(); });
282 m.def(
"return_capsule_with_destructor", []() {
284 return py::capsule([]() {
py::print(
"destructing capsule"); });
287 m.def(
"return_renamed_capsule_with_destructor", []() {
289 auto cap = py::capsule([]() {
py::print(
"destructing capsule"); });
290 static const char *capsule_name =
"test_name1";
292 cap.set_name(capsule_name);
296 m.def(
"return_capsule_with_destructor_2", []() {
298 return py::capsule((
void *) 1234, [](
void *ptr) {
299 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
303 m.def(
"return_capsule_with_destructor_3", []() {
305 auto cap = py::capsule((
void *) 1233,
"oname", [](
void *ptr) {
306 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
308 py::print(
"original name: {}"_s.format(cap.name()));
312 m.def(
"return_renamed_capsule_with_destructor_2", []() {
314 auto cap = py::capsule((
void *) 1234, [](
void *ptr) {
315 py::print(
"destructing capsule: {}"_s.format((
size_t) ptr));
317 static const char *capsule_name =
"test_name2";
319 cap.set_name(capsule_name);
323 m.def(
"return_capsule_with_name_and_destructor", []() {
324 auto capsule = py::capsule((
void *) 12345,
"pointer type description", [](PyObject *ptr) {
326 const auto *
name = PyCapsule_GetName(ptr);
327 py::print(
"destructing capsule ({}, '{}')"_s.format(
328 (
size_t) PyCapsule_GetPointer(ptr,
name),
name));
332 capsule.set_pointer((
void *) 1234);
335 void *contents1 =
static_cast<void *
>(
capsule);
336 void *contents2 =
capsule.get_pointer();
337 void *contents3 =
capsule.get_pointer<
void>();
339 auto result1 =
reinterpret_cast<size_t>(contents1);
340 auto result2 =
reinterpret_cast<size_t>(contents2);
341 auto result3 =
reinterpret_cast<size_t>(contents3);
344 "created capsule ({}, '{}')"_s.format(result1 & result2 & result3,
capsule.name()));
348 m.def(
"return_capsule_with_explicit_nullptr_dtor", []() {
349 py::print(
"creating capsule with explicit nullptr dtor");
350 return py::capsule(
reinterpret_cast<void *
>(1234),
351 static_cast<void (*)(
void *)
>(
nullptr));
355 m.def(
"accessor_api", [](
const py::object &o) {
358 d[
"basic_attr"] = o.attr(
"basic_attr");
361 for (
auto item : o.attr(
"begin_end")) {
366 d[
"operator[object]"] = o.attr(
"d")[
"operator[object]"_s];
367 d[
"operator[char *]"] = o.attr(
"d")[
"operator[char *]"];
369 d[
"attr(object)"] = o.attr(
"sub").attr(
"attr_obj");
370 d[
"attr(char *)"] = o.attr(
"sub").attr(
"attr_char");
372 o.attr(
"sub").attr(
"missing").ptr();
373 }
catch (
const py::error_already_set &) {
374 d[
"missing_attr_ptr"] =
"raised"_s;
377 o.attr(
"missing").attr(
"doesn't matter");
378 }
catch (
const py::error_already_set &) {
379 d[
"missing_attr_chain"] =
"raised"_s;
382 d[
"is_none"] = o.attr(
"basic_attr").is_none();
384 d[
"operator()"] = o.attr(
"func")(1);
385 d[
"operator*"] = o.attr(
"func")(*o.attr(
"begin_end"));
388 py::list implicit_list = o.attr(
"begin_end");
389 d[
"implicit_list"] = implicit_list;
390 py::dict implicit_dict = o.attr(
"__dict__");
391 d[
"implicit_dict"] = implicit_dict;
396 m.def(
"tuple_accessor", [](
const py::tuple &existing_t) {
399 }
catch (
const py::error_already_set &) {
402 auto new_t = py::tuple(3);
403 for (
size_t i = 0;
i < new_t.size(); ++
i) {
411 m.def(
"accessor_assignment", []() {
412 auto l = py::list(1);
418 d[
"deferred_get"] = var;
422 d[
"deferred_set"] =
l[0];
428 m.def(
"accessor_moves", []() {
429 py::list return_list;
430 #ifdef PYBIND11_HANDLE_REF_DEBUG
431 py::int_ py_int_0(0);
432 py::int_ py_int_42(42);
437 py::sequence
seq(tup);
442 # define PYBIND11_LOCAL_DEF(...) \
444 std::size_t inc_refs = py::handle::inc_ref_counter(); \
446 inc_refs = py::handle::inc_ref_counter() - inc_refs; \
447 return_list.append(inc_refs); \
450 PYBIND11_LOCAL_DEF(tup[py_int_0])
451 PYBIND11_LOCAL_DEF(tup[py::int_(0)])
453 PYBIND11_LOCAL_DEF(tup.attr(py_str_count))
454 PYBIND11_LOCAL_DEF(tup.attr(
py::str(
"count")))
456 PYBIND11_LOCAL_DEF(
seq[py_int_0])
457 PYBIND11_LOCAL_DEF(
seq[py::int_(0)])
459 PYBIND11_LOCAL_DEF(
seq.attr(py_str_count))
460 PYBIND11_LOCAL_DEF(
seq.attr(
py::str(
"count")))
462 PYBIND11_LOCAL_DEF(lst[py_int_0])
463 PYBIND11_LOCAL_DEF(lst[py::int_(0)])
465 PYBIND11_LOCAL_DEF(lst.attr(py_str_count))
466 PYBIND11_LOCAL_DEF(lst.attr(
py::str(
"count")))
468 auto lst_acc = lst[py::int_(0)];
469 lst_acc = py::int_(42);
470 PYBIND11_LOCAL_DEF(lst_acc = py_int_42)
471 PYBIND11_LOCAL_DEF(lst_acc = py::int_(42))
472 # undef PYBIND11_LOCAL_DEF
478 m.def(
"default_constructors", []() {
479 return py::dict(
"bytes"_a = py::bytes(),
480 "bytearray"_a = py::bytearray(),
482 "bool"_a = py::bool_(),
483 "int"_a = py::int_(),
484 "float"_a = py::float_(),
485 "tuple"_a = py::tuple(),
486 "list"_a = py::list(),
487 "dict"_a = py::dict(),
491 m.def(
"converting_constructors", [](
const py::dict &
d) {
492 return py::dict(
"bytes"_a = py::bytes(
d[
"bytes"]),
493 "bytearray"_a = py::bytearray(
d[
"bytearray"]),
495 "bool"_a = py::bool_(
d[
"bool"]),
496 "int"_a = py::int_(
d[
"int"]),
497 "float"_a = py::float_(
d[
"float"]),
498 "tuple"_a = py::tuple(
d[
"tuple"]),
499 "list"_a = py::list(
d[
"list"]),
500 "dict"_a = py::dict(
d[
"dict"]),
502 "frozenset"_a = py::frozenset(
d[
"frozenset"]),
503 "memoryview"_a = py::memoryview(
d[
"memoryview"]));
506 m.def(
"cast_functions", [](
const py::dict &
d) {
508 return py::dict(
"bytes"_a =
d[
"bytes"].cast<py::bytes>(),
509 "bytearray"_a =
d[
"bytearray"].cast<py::bytearray>(),
510 "str"_a =
d[
"str"].cast<py::str>(),
511 "bool"_a =
d[
"bool"].cast<py::bool_>(),
512 "int"_a =
d[
"int"].cast<py::int_>(),
513 "float"_a =
d[
"float"].cast<py::float_>(),
514 "tuple"_a =
d[
"tuple"].cast<py::tuple>(),
515 "list"_a =
d[
"list"].cast<py::list>(),
516 "dict"_a =
d[
"dict"].cast<py::dict>(),
517 "set"_a =
d[
"set"].cast<py::set>(),
518 "frozenset"_a =
d[
"frozenset"].cast<py::frozenset>(),
519 "memoryview"_a =
d[
"memoryview"].cast<py::memoryview>());
522 m.def(
"convert_to_pybind11_str", [](
const py::object &o) {
return py::str(o); });
524 m.def(
"nonconverting_constructor",
525 [](
const std::string &
type, py::object
value,
bool move) -> py::object {
526 if (
type ==
"bytes") {
529 if (
type ==
"none") {
532 if (
type ==
"ellipsis") {
533 return move ? py::ellipsis(std::move(
value)) : py::ellipsis(
value);
535 if (
type ==
"type") {
538 throw std::runtime_error(
"Invalid type");
541 m.def(
"get_implicit_casting", []() {
543 d[
"char*_i1"] =
"abc";
544 const char *
c2 =
"abc";
555 d[
"int_p"] = py::int_(
i);
557 d[
"str_i1"] = std::string(
"str");
558 std::string s2(
"str1");
570 l.append(py::int_(15));
572 return py::dict(
"d"_a =
d,
"l"_a =
l);
576 m.def(
"print_function", []() {
578 py::print(1, 2.0,
"three",
true, std::string(
"-- multiple args"));
581 py::print(
"no new line here",
"end"_a =
" -- ");
584 auto py_stderr = py::module_::import(
"sys").attr(
"stderr");
585 py::print(
"this goes to stderr",
"file"_a = py_stderr);
590 "{a} + {b} = {c}"_s.format(
"a"_a =
"py::print",
"b"_a =
"str.format",
"c"_a =
"this"));
595 m.def(
"hash_function", [](py::object obj) {
return py::hash(std::move(obj)); });
597 m.def(
"obj_contains",
598 [](py::object &obj,
const py::object &
key) {
return obj.contains(
key); });
600 m.def(
"test_number_protocol", [](
const py::object &
a,
const py::object &
b) {
602 l.append(
a.equal(
b));
603 l.append(
a.not_equal(
b));
620 m.def(
"test_list_slicing", [](
const py::list &
a) {
return a[py::slice(0, -1, 2)]; });
623 m.def(
"issue2361_str_implicit_copy_none", []() {
624 py::str is_this_none = py::none();
627 m.def(
"issue2361_dict_implicit_copy_none", []() {
628 py::dict is_this_none = py::none();
632 m.def(
"test_memoryview_object", [](
const py::buffer &
b) {
return py::memoryview(
b); });
634 m.def(
"test_memoryview_buffer_info",
635 [](
const py::buffer &
b) {
return py::memoryview(
b.request()); });
637 m.def(
"test_memoryview_from_buffer", [](
bool is_unsigned) {
638 static const int16_t si16[] = {3, 1, 4, 1, 5};
639 static const uint16_t ui16[] = {2, 7, 1, 8};
641 return py::memoryview::from_buffer(ui16, {4}, {
sizeof(
uint16_t)});
643 return py::memoryview::from_buffer(si16, {5}, {
sizeof(
int16_t)});
646 m.def(
"test_memoryview_from_buffer_nativeformat", []() {
647 static const char *
format =
"@i";
652 m.def(
"test_memoryview_from_buffer_empty_shape", []() {
653 static const char *buf =
"";
654 return py::memoryview::from_buffer(buf, 1,
"B", {}, {});
657 m.def(
"test_memoryview_from_buffer_invalid_strides", []() {
658 static const char *buf =
"\x02\x03\x04";
659 return py::memoryview::from_buffer(buf, 1,
"B", {3}, {});
662 m.def(
"test_memoryview_from_buffer_nullptr", []() {
663 return py::memoryview::from_buffer(
static_cast<void *
>(
nullptr), 1,
"B", {}, {});
666 m.def(
"test_memoryview_from_memory", []() {
667 const char *buf =
"\xff\xe1\xab\x37";
668 return py::memoryview::from_memory(buf,
static_cast<py::ssize_t>(strlen(buf)));
672 m.def(
"get_len", [](py::handle
h) {
return py::len(
h); });
674 #ifdef PYBIND11_STR_LEGACY_PERMISSIVE
675 m.attr(
"PYBIND11_STR_LEGACY_PERMISSIVE") =
true;
678 m.def(
"isinstance_pybind11_bytes",
679 [](py::object o) {
return py::isinstance<py::bytes>(std::move(o)); });
680 m.def(
"isinstance_pybind11_str",
681 [](py::object o) {
return py::isinstance<py::str>(std::move(o)); });
683 m.def(
"pass_to_pybind11_bytes", [](py::bytes
b) {
return py::len(std::move(
b)); });
685 m.def(
"pass_to_std_string", [](
const std::string &
s) {
return s.size(); });
688 m.def(
"weakref_from_handle", [](py::handle
h) {
return py::weakref(
h); });
689 m.def(
"weakref_from_handle_and_function",
690 [](py::handle
h, py::function
f) {
return py::weakref(
h, std::move(
f)); });
691 m.def(
"weakref_from_object", [](
const py::object &o) {
return py::weakref(o); });
692 m.def(
"weakref_from_object_and_function",
693 [](py::object o, py::function
f) {
return py::weakref(std::move(o), std::move(
f)); });
699 #if (defined(__APPLE__) && defined(__clang__)) || defined(PYPY_VERSION)
701 # define PYBIND11_AUTO_IT auto it
705 # define PYBIND11_AUTO_IT auto &it
708 m.def(
"tuple_iterator", []() {
712 tup_sum += it.cast<
int>();
717 m.def(
"dict_iterator", []() {
719 dct[py::int_(3)] = 5;
720 dct[py::int_(7)] = 11;
723 kv_sum += it.first.cast<
int>() * 100 + it.second.cast<
int>();
728 m.def(
"passed_iterator", [](
const py::iterator &py_it) {
731 elem_sum += it.cast<
int>();
736 #undef PYBIND11_AUTO_IT
740 m.def(
"sequence_item_get_ssize_t", [](
const py::object &o) {
743 m.def(
"sequence_item_set_ssize_t", [](
const py::object &o) {
747 m.def(
"sequence_item_get_size_t", [](
const py::object &o) {
750 m.def(
"sequence_item_set_size_t", [](
const py::object &o) {
754 m.def(
"list_item_get_ssize_t", [](
const py::object &o) {
757 m.def(
"list_item_set_ssize_t", [](
const py::object &o) {
761 m.def(
"list_item_get_size_t", [](
const py::object &o) {
764 m.def(
"list_item_set_size_t", [](
const py::object &o) {
768 m.def(
"tuple_item_get_ssize_t", [](
const py::object &o) {
771 m.def(
"tuple_item_set_ssize_t", []() {
773 auto s1 =
py::str{
"edmond", 6};
774 auto o = py::tuple{2};
779 m.def(
"tuple_item_get_size_t", [](
const py::object &o) {
782 m.def(
"tuple_item_set_size_t", []() {
785 auto o = py::tuple{2};
792 double v =
x.get_value();
796 m.def(
"tuple_rvalue_getter", [](
const py::tuple &tup) {
798 for (
size_t i = 0;
i < tup.size();
i++) {
799 auto o = py::handle(tup[py::int_(
i)]);
801 throw py::value_error(
"tuple is malformed");
806 m.def(
"list_rvalue_getter", [](
const py::list &
l) {
808 for (
size_t i = 0;
i <
l.size();
i++) {
809 auto o = py::handle(
l[py::int_(
i)]);
811 throw py::value_error(
"list is malformed");
816 m.def(
"populate_dict_rvalue", [](
int population) {
818 for (
int i = 0;
i < population;
i++) {
819 d[py::int_(
i)] = py::int_(
i);
823 m.def(
"populate_obj_str_attrs", [](py::object &o,
int population) {
824 for (
int i = 0;
i < population;
i++) {
831 m.def(
"inplace_append", [](py::object &
a,
const py::object &
b) {
835 m.def(
"inplace_subtract", [](py::object &
a,
const py::object &
b) {
839 m.def(
"inplace_multiply", [](py::object &
a,
const py::object &
b) {
843 m.def(
"inplace_divide", [](py::object &
a,
const py::object &
b) {
847 m.def(
"inplace_or", [](py::object &
a,
const py::object &
b) {
851 m.def(
"inplace_and", [](py::object &
a,
const py::object &
b) {
855 m.def(
"inplace_lshift", [](py::object &
a,
const py::object &
b) {
859 m.def(
"inplace_rshift", [](py::object &
a,
const py::object &
b) {
864 m.def(
"annotate_tuple_float_str", [](
const py::typing::Tuple<py::float_, py::str> &) {});
865 m.def(
"annotate_tuple_empty", [](
const py::typing::Tuple<> &) {});
866 m.def(
"annotate_tuple_variable_length",
867 [](
const py::typing::Tuple<py::float_, py::ellipsis> &) {});
868 m.def(
"annotate_dict_str_int", [](
const py::typing::Dict<py::str, int> &) {});
869 m.def(
"annotate_list_int", [](
const py::typing::List<int> &) {});
870 m.def(
"annotate_set_str", [](
const py::typing::Set<std::string> &) {});
871 m.def(
"annotate_iterable_str", [](
const py::typing::Iterable<std::string> &) {});
872 m.def(
"annotate_iterator_int", [](
const py::typing::Iterator<int> &) {});
874 [](
const py::typing::Callable<
int(py::typing::List<py::str>,
py::str)> &) {});
876 m.def(
"annotate_fn_only_return", [](
const py::typing::Callable<
int(py::ellipsis)> &) {});
877 m.def(
"annotate_type", [](
const py::typing::Type<int> &
t) ->
py::type {
return t; });
879 m.def(
"annotate_union",
880 [](py::typing::List<py::typing::Union<py::str, py::int_, py::object>>
l,
883 py::object
c) -> py::typing::List<py::typing::Union<py::str, py::int_, py::object>> {
890 m.def(
"union_typing_only",
891 [](py::typing::List<py::typing::Union<py::str>> &
l)
892 -> py::typing::List<py::typing::Union<py::int_>> {
return l; });
894 m.def(
"annotate_union_to_object",
895 [](py::typing::Union<int, py::str> &o) -> py::object {
return o; });
897 m.def(
"annotate_optional",
898 [](py::list &list) -> py::typing::List<py::typing::Optional<py::str>> {
900 list.append(py::none());
904 m.def(
"annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard<py::str> {
905 return py::isinstance<py::str>(o);
907 m.def(
"annotate_type_is",
908 [](py::object &o) -> py::typing::TypeIs<py::str> {
return py::isinstance<py::str>(o); });
910 m.def(
"annotate_no_return", []() -> py::typing::NoReturn {
throw 0; });
911 m.def(
"annotate_never", []() -> py::typing::Never {
throw 0; });
913 m.def(
"annotate_optional_to_object",
914 [](py::typing::Optional<int> &o) -> py::object {
return o; });
916 #if defined(PYBIND11_TYPING_H_HAS_STRING_LITERAL)
917 py::enum_<literals::Color>(
m,
"Color")
918 .value(
"RED", literals::Color::RED)
919 .value(
"BLUE", literals::Color::BLUE);
921 m.def(
"annotate_literal", [](literals::LiteralFoo &o) -> py::object {
return o; });
922 m.def(
"annotate_generic_containers",
923 [](
const py::typing::List<typevar::TypeVarT> &
l) -> py::typing::List<typevar::TypeVarV> {
927 m.def(
"annotate_listT_to_T",
928 [](
const py::typing::List<typevar::TypeVarT> &
l) -> typevar::TypeVarT {
return l[0]; });
929 m.def(
"annotate_object_to_T", [](
const py::object &o) -> typevar::TypeVarT {
return o; });
930 m.attr(
"defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") =
true;
932 m.attr(
"defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") =
false;
935 #if defined(PYBIND11_TEST_PYTYPES_HAS_RANGES)
938 m.def(
"tuple_iterator_default_initialization", []() {
939 using TupleIterator = decltype(std::declval<py::tuple>().begin());
940 static_assert(std::random_access_iterator<TupleIterator>);
941 return TupleIterator{} == TupleIterator{};
944 m.def(
"transform_tuple_plus_one", [](py::tuple &tpl) {
947 ret.append(py::int_(it));
953 m.def(
"list_iterator_default_initialization", []() {
954 using ListIterator = decltype(std::declval<py::list>().begin());
955 static_assert(std::random_access_iterator<ListIterator>);
956 return ListIterator{} == ListIterator{};
959 m.def(
"transform_list_plus_one", [](py::list &lst) {
962 ret.append(py::int_(it));
968 m.def(
"dict_iterator_default_initialization", []() {
969 using DictIterator = decltype(std::declval<py::dict>().begin());
970 static_assert(std::forward_iterator<DictIterator>);
971 return DictIterator{} == DictIterator{};
974 m.def(
"transform_dict_plus_one", [](py::dict &dct) {
977 return std::pair{py::cast<int>(o.first) + 1,
978 py::cast<int>(o.second) + 1};
985 m.attr(
"defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") =
true;
987 m.attr(
"defined_PYBIND11_TEST_PYTYPES_HAS_RANGES") =
false;