From d872367cfdb1cd3d183a7e6341dfa00cd10f5fb1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:13:53 -0400 Subject: [PATCH 01/50] typevar prototype --- include/pybind11/typing.h | 10 ++++++++++ tests/test_pytypes.cpp | 8 ++++++++ tests/test_pytypes.py | 14 ++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index bc275fc50b..5e25d526a2 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,6 +63,11 @@ class Callable : public function { using function::function; }; +template +class TypeVar : public T { + using T::T; +}; + PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) @@ -121,5 +126,10 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; +template +struct handle_type_name { + static constexpr auto name = const_name(NameT); +}; + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index f3709d40c6..98ebfd747a 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,6 +109,11 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject +namespace typevar{ + typedef py::typing::TypeVar<"T", py::str> TypeVarT; + typedef py::typing::TypeVar<"V", py::str> TypeVarV; +} + TEST_SUBMODULE(pytypes, m) { m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); }); @@ -844,4 +849,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_iterator_int", [](const py::typing::Iterator &) {}); m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); + + m.def("annotate_fn_generics", [](const py::typing::List &) -> typevar::TypeVarT {}); + m.def("annotate_fn_different_generics", [](const typevar::TypeVarT &a) -> typevar::TypeVarV { return a; }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 38edfd9998..d72d8c353d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -955,3 +955,17 @@ def test_fn_annotations(doc): doc(m.annotate_fn) == "annotate_fn(arg0: Callable[[list[str], str], int]) -> None" ) + + +def test_fn_generics(doc): + assert ( + doc(m.annotate_fn_generics) + == "annotate_fn_generics(arg0: list[T]) -> T" + ) + + +def test_fn_generics_uniqueness(doc): + assert ( + doc(m.annotate_fn_different_generics) + == "annotate_fn_different_generics(arg0: T) -> V" + ) From 1012f1099186aa0922343576d41410aa6ae8f16b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:15:24 +0000 Subject: [PATCH 02/50] style: pre-commit fixes --- include/pybind11/typing.h | 2 +- tests/test_pytypes.cpp | 14 ++++++++------ tests/test_pytypes.py | 5 +---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 5e25d526a2..f694ac1e67 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,7 +127,7 @@ struct handle_type_name> { }; template -struct handle_type_name { + struct handle_type_name < typing::TypeVar { static constexpr auto name = const_name(NameT); }; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 98ebfd747a..d792892996 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,10 +109,10 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject -namespace typevar{ - typedef py::typing::TypeVar<"T", py::str> TypeVarT; - typedef py::typing::TypeVar<"V", py::str> TypeVarV; -} +namespace typevar { +typedef py::typing::TypeVar<"T", py::str> TypeVarT; +typedef py::typing::TypeVar<"V", py::str> TypeVarV; +} // namespace typevar TEST_SUBMODULE(pytypes, m) { m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); }); @@ -850,6 +850,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); - m.def("annotate_fn_generics", [](const py::typing::List &) -> typevar::TypeVarT {}); - m.def("annotate_fn_different_generics", [](const typevar::TypeVarT &a) -> typevar::TypeVarV { return a; }); + m.def("annotate_fn_generics", + [](const py::typing::List &) -> typevar::TypeVarT {}); + m.def("annotate_fn_different_generics", + [](const typevar::TypeVarT &a) -> typevar::TypeVarV { return a; }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index d72d8c353d..6443173eee 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,10 +958,7 @@ def test_fn_annotations(doc): def test_fn_generics(doc): - assert ( - doc(m.annotate_fn_generics) - == "annotate_fn_generics(arg0: list[T]) -> T" - ) + assert doc(m.annotate_fn_generics) == "annotate_fn_generics(arg0: list[T]) -> T" def test_fn_generics_uniqueness(doc): From fbd168af54896b278ca542899b7505f58e9a0696 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:16:51 -0400 Subject: [PATCH 03/50] change to NameT --- include/pybind11/typing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index f694ac1e67..4cbe1412a1 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -template +template class TypeVar : public T { using T::T; }; @@ -127,7 +127,7 @@ struct handle_type_name> { }; template - struct handle_type_name < typing::TypeVar { +struct handle_type_name { static constexpr auto name = const_name(NameT); }; From 34bf41ba9168cfff0365197d736fb27f54a1aa16 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:17:12 +0000 Subject: [PATCH 04/50] style: pre-commit fixes --- include/pybind11/typing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 4cbe1412a1..537dcf8048 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,7 +127,7 @@ struct handle_type_name> { }; template -struct handle_type_name { + struct handle_type_name < typing::TypeVar { static constexpr auto name = const_name(NameT); }; From 63acf62079a137f5d1bc72765016a6cdf5fa406c Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:20:46 -0400 Subject: [PATCH 05/50] make string const --- include/pybind11/typing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 537dcf8048..1a2cd06c17 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -template +template class TypeVar : public T { using T::T; }; @@ -126,7 +126,7 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -template +template struct handle_type_name < typing::TypeVar { static constexpr auto name = const_name(NameT); }; From 38fd3578526596ae5f8efca681dbc924362a5c43 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:22:02 -0400 Subject: [PATCH 06/50] add missing closing bracket --- include/pybind11/typing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 1a2cd06c17..0180d746dd 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,7 +127,7 @@ struct handle_type_name> { }; template - struct handle_type_name < typing::TypeVar { + struct handle_type_name < typing::TypeVar> { static constexpr auto name = const_name(NameT); }; From 614d03e778cb432ae61b59786ecd646a648d17e1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:22:24 +0000 Subject: [PATCH 07/50] style: pre-commit fixes --- include/pybind11/typing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 0180d746dd..3ceba4a156 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,7 +127,7 @@ struct handle_type_name> { }; template - struct handle_type_name < typing::TypeVar> { +struct handle_type_name> { static constexpr auto name = const_name(NameT); }; From d3545121df259e145ba21520b73bff7f108cbec1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:22:32 -0400 Subject: [PATCH 08/50] clean up handle_type_name --- include/pybind11/typing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 0180d746dd..19330ea0f1 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,7 +127,7 @@ struct handle_type_name> { }; template - struct handle_type_name < typing::TypeVar> { + struct handle_type_name < typing::TypeVar> { static constexpr auto name = const_name(NameT); }; From a1981524dd9955d33fda3aaea8efd1dcde48fe01 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:23:27 +0000 Subject: [PATCH 09/50] style: pre-commit fixes --- include/pybind11/typing.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 76dd3c7d8a..d4a6c39d5c 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,7 +127,8 @@ struct handle_type_name> { }; template -struct handle_type_name> { + struct handle_type_name < typing::TypeVarNameT, + T >> { static constexpr auto name = const_name(NameT); }; From ee00f706d097159ac5dd9dd22395c95630c9381b Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:25:09 -0400 Subject: [PATCH 10/50] add back missing < --- include/pybind11/typing.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index d4a6c39d5c..1d968c8934 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -template +template class TypeVar : public T { using T::T; }; @@ -126,8 +126,8 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -template - struct handle_type_name < typing::TypeVarNameT, +template + struct handle_type_name < typing::TypeVar> { static constexpr auto name = const_name(NameT); }; From e19ee2298cf30c45eb5ed36b1b64a005eb834984 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:25:30 +0000 Subject: [PATCH 11/50] style: pre-commit fixes --- include/pybind11/typing.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 1d968c8934..9997631741 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,8 +127,7 @@ struct handle_type_name> { }; template - struct handle_type_name < typing::TypeVar> { +struct handle_type_name> { static constexpr auto name = const_name(NameT); }; From 98f836e35b8527383d166b57cc8b752af1b45db8 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:26:21 -0400 Subject: [PATCH 12/50] add back NameT --- include/pybind11/typing.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 9997631741..8b8fd262f2 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -template +template class TypeVar : public T { using T::T; }; @@ -126,8 +126,8 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -template -struct handle_type_name> { +template +struct handle_type_name> { static constexpr auto name = const_name(NameT); }; From e1dc7a5c783726040a60aedc84350202d60f19ea Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:42:17 -0400 Subject: [PATCH 13/50] try fixed_string --- include/pybind11/typing.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 8b8fd262f2..946ec6b994 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,9 +63,11 @@ class Callable : public function { using function::function; }; -template + +template class TypeVar : public T { using T::T; + // constexpr TypeVar(const std::string) }; PYBIND11_NAMESPACE_END(typing) @@ -126,7 +128,7 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -template +template struct handle_type_name> { static constexpr auto name = const_name(NameT); }; From 14ea23d6c947e1eb51bb000c2a0b51dd442302bc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 03:42:43 +0000 Subject: [PATCH 14/50] style: pre-commit fixes --- include/pybind11/typing.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 946ec6b994..e662f9d106 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,6 @@ class Callable : public function { using function::function; }; - template class TypeVar : public T { using T::T; From 5e5066cc9df4ee9d39ef0f4313e9e8a22d6f3ba5 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:48:29 -0400 Subject: [PATCH 15/50] std::basic_fixed_string --- include/pybind11/typing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index e662f9d106..fdfca0d54a 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -template +template class TypeVar : public T { using T::T; // constexpr TypeVar(const std::string) @@ -127,7 +127,7 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -template +template struct handle_type_name> { static constexpr auto name = const_name(NameT); }; From 534dd65d817fa42d6fd5b1b465e1e3dbb65d9dfb Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 12:14:13 -0400 Subject: [PATCH 16/50] test c++20 --- include/pybind11/typing.h | 36 +++++++++++++++++++++++++++++------- tests/test_pytypes.cpp | 20 ++++++++++++++------ tests/test_pytypes.py | 6 +++--- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index fdfca0d54a..f81051f812 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,10 +63,31 @@ class Callable : public function { using function::function; }; -template -class TypeVar : public T { - using T::T; - // constexpr TypeVar(const std::string) +template +struct StringLiteral { + constexpr StringLiteral(const char (&str)[N]) { + std::copy_n(str, N, value); + } + + char value[N]; +}; + + +template +class TypeVar : public object { + // public: + // constexpr auto name = lit.value; +// const char name[N]; +// // TODO likely use this vv +// // PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyTypeVar_Check) +// constexpr TypeVar(const char (&s)[N]){ +// std::copy_n(s, s + N, name); +// } +// // // Very hacky +// // bool check_(handle &){ +// // // Check type then name? +// // return false; +// // }; }; PYBIND11_NAMESPACE_END(typing) @@ -127,9 +148,10 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -template -struct handle_type_name> { - static constexpr auto name = const_name(NameT); +template +struct handle_type_name> { + // static constexpr auto name = const_name("TP"); + static constexpr auto name = const_name(lit.value); }; PYBIND11_NAMESPACE_END(detail) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index d792892996..20d4dfafc0 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -110,8 +110,15 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject namespace typevar { -typedef py::typing::TypeVar<"T", py::str> TypeVarT; -typedef py::typing::TypeVar<"V", py::str> TypeVarV; + // const char t[] = "T"; + // const char v[] = "V"; + + // typedef py::typing::TypeVar TypeVarT; + // typedef py::typing::TypeVar TypeVarV; + + + typedef py::typing::TypeVar<"T"> TypeVarT; + typedef py::typing::TypeVar<"V"> TypeVarV; } // namespace typevar TEST_SUBMODULE(pytypes, m) { @@ -850,8 +857,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); - m.def("annotate_fn_generics", - [](const py::typing::List &) -> typevar::TypeVarT {}); - m.def("annotate_fn_different_generics", - [](const typevar::TypeVarT &a) -> typevar::TypeVarV { return a; }); + m.def("annotate_generic_containers", + [](const py::typing::List &l) -> py::typing::List {return l;}); + // TODO T -> T + m.def("annotate_listT_to_T", + [](const py::typing::List &l) -> py::object { return l[0]; }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 6443173eee..b14283854e 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,11 +958,11 @@ def test_fn_annotations(doc): def test_fn_generics(doc): - assert doc(m.annotate_fn_generics) == "annotate_fn_generics(arg0: list[T]) -> T" + assert doc(m.annotate_generic_containers) == "annotate_generic_containers(arg0: list[T]) -> list[V]" def test_fn_generics_uniqueness(doc): assert ( - doc(m.annotate_fn_different_generics) - == "annotate_fn_different_generics(arg0: T) -> V" + doc(m.annotate_listT_to_T) + == "annotate_typevar_T_to_T(arg0: list[T]) -> T" ) From 651227fd87a465f03473b4f3daf938bb14f791cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:14:37 +0000 Subject: [PATCH 17/50] style: pre-commit fixes --- include/pybind11/typing.h | 33 +++++++++++++++------------------ tests/test_pytypes.cpp | 17 +++++++++-------- tests/test_pytypes.py | 10 +++++----- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index f81051f812..e2bf880d21 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,31 +63,28 @@ class Callable : public function { using function::function; }; -template +template struct StringLiteral { - constexpr StringLiteral(const char (&str)[N]) { - std::copy_n(str, N, value); - } - + constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } + char value[N]; }; - template class TypeVar : public object { - // public: + // public: // constexpr auto name = lit.value; -// const char name[N]; -// // TODO likely use this vv -// // PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyTypeVar_Check) -// constexpr TypeVar(const char (&s)[N]){ -// std::copy_n(s, s + N, name); -// } -// // // Very hacky -// // bool check_(handle &){ -// // // Check type then name? -// // return false; -// // }; + // const char name[N]; + // // TODO likely use this vv + // // PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyTypeVar_Check) + // constexpr TypeVar(const char (&s)[N]){ + // std::copy_n(s, s + N, name); + // } + // // // Very hacky + // // bool check_(handle &){ + // // // Check type then name? + // // return false; + // // }; }; PYBIND11_NAMESPACE_END(typing) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 20d4dfafc0..52a50805d6 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -110,15 +110,14 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject namespace typevar { - // const char t[] = "T"; - // const char v[] = "V"; +// const char t[] = "T"; +// const char v[] = "V"; - // typedef py::typing::TypeVar TypeVarT; - // typedef py::typing::TypeVar TypeVarV; +// typedef py::typing::TypeVar TypeVarT; +// typedef py::typing::TypeVar TypeVarV; - - typedef py::typing::TypeVar<"T"> TypeVarT; - typedef py::typing::TypeVar<"V"> TypeVarV; +typedef py::typing::TypeVar<"T"> TypeVarT; +typedef py::typing::TypeVar<"V"> TypeVarV; } // namespace typevar TEST_SUBMODULE(pytypes, m) { @@ -858,7 +857,9 @@ TEST_SUBMODULE(pytypes, m) { [](const py::typing::Callable, py::str)> &) {}); m.def("annotate_generic_containers", - [](const py::typing::List &l) -> py::typing::List {return l;}); + [](const py::typing::List &l) -> py::typing::List { + return l; + }); // TODO T -> T m.def("annotate_listT_to_T", [](const py::typing::List &l) -> py::object { return l[0]; }); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index b14283854e..6bcd6fdaff 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,11 +958,11 @@ def test_fn_annotations(doc): def test_fn_generics(doc): - assert doc(m.annotate_generic_containers) == "annotate_generic_containers(arg0: list[T]) -> list[V]" + assert ( + doc(m.annotate_generic_containers) + == "annotate_generic_containers(arg0: list[T]) -> list[V]" + ) def test_fn_generics_uniqueness(doc): - assert ( - doc(m.annotate_listT_to_T) - == "annotate_typevar_T_to_T(arg0: list[T]) -> T" - ) + assert doc(m.annotate_listT_to_T) == "annotate_typevar_T_to_T(arg0: list[T]) -> T" From 0f8864cb843512d149b432755ce3f78474323207 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 12:48:00 -0400 Subject: [PATCH 18/50] cleanup --- include/pybind11/typing.h | 17 +---------------- tests/test_pytypes.py | 2 +- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index e2bf880d21..c46a08dc08 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -71,21 +71,7 @@ struct StringLiteral { }; template -class TypeVar : public object { - // public: - // constexpr auto name = lit.value; - // const char name[N]; - // // TODO likely use this vv - // // PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyTypeVar_Check) - // constexpr TypeVar(const char (&s)[N]){ - // std::copy_n(s, s + N, name); - // } - // // // Very hacky - // // bool check_(handle &){ - // // // Check type then name? - // // return false; - // // }; -}; +class TypeVar : public object {}; PYBIND11_NAMESPACE_END(typing) @@ -147,7 +133,6 @@ struct handle_type_name> { template struct handle_type_name> { - // static constexpr auto name = const_name("TP"); static constexpr auto name = const_name(lit.value); }; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 6bcd6fdaff..b5af3778c0 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -965,4 +965,4 @@ def test_fn_generics(doc): def test_fn_generics_uniqueness(doc): - assert doc(m.annotate_listT_to_T) == "annotate_typevar_T_to_T(arg0: list[T]) -> T" + assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" From b5499023b052b1a79cfc904de01994780e645b5c Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 13:09:06 -0400 Subject: [PATCH 19/50] fix object to typevar conversion --- include/pybind11/typing.h | 5 ++++- tests/test_pytypes.cpp | 11 +++-------- tests/test_pytypes.py | 4 ++++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index c46a08dc08..d1e921a4e5 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -71,7 +71,10 @@ struct StringLiteral { }; template -class TypeVar : public object {}; +class TypeVar : public object { + PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) + using object::object; +}; PYBIND11_NAMESPACE_END(typing) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 52a50805d6..015c45d7ec 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -110,12 +110,6 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject namespace typevar { -// const char t[] = "T"; -// const char v[] = "V"; - -// typedef py::typing::TypeVar TypeVarT; -// typedef py::typing::TypeVar TypeVarV; - typedef py::typing::TypeVar<"T"> TypeVarT; typedef py::typing::TypeVar<"V"> TypeVarV; } // namespace typevar @@ -860,7 +854,8 @@ TEST_SUBMODULE(pytypes, m) { [](const py::typing::List &l) -> py::typing::List { return l; }); - // TODO T -> T m.def("annotate_listT_to_T", - [](const py::typing::List &l) -> py::object { return l[0]; }); + [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); + m.def("annotate_object_to_T", + [](const py::object &o) -> typevar::TypeVarT { return o; }); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index b5af3778c0..6318090bdd 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -966,3 +966,7 @@ def test_fn_generics(doc): def test_fn_generics_uniqueness(doc): assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" + + +def test_object_and_typevar_equilvance(doc): + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From a858638c59788fd164a06abe67e079dd570323e0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:09:30 +0000 Subject: [PATCH 20/50] style: pre-commit fixes --- tests/test_pytypes.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 015c45d7ec..46d798db70 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -856,6 +856,5 @@ TEST_SUBMODULE(pytypes, m) { }); m.def("annotate_listT_to_T", [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); - m.def("annotate_object_to_T", - [](const py::object &o) -> typevar::TypeVarT { return o; }); + m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); } From 0380dc1bd24641f9d77f1c07652df26a5bd6957b Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 15:22:37 -0400 Subject: [PATCH 21/50] And CPP20 checks --- include/pybind11/typing.h | 4 ++++ tests/test_pytypes.cpp | 14 +++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index d1e921a4e5..d363d91567 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,6 +63,7 @@ class Callable : public function { using function::function; }; +#if defined(PYBIND11_CPP20) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } @@ -75,6 +76,7 @@ class TypeVar : public object { PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) using object::object; }; +#endif PYBIND11_NAMESPACE_END(typing) @@ -134,10 +136,12 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; +#if defined(PYBIND11_CPP20) template struct handle_type_name> { static constexpr auto name = const_name(lit.value); }; +#endif PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 46d798db70..245197172a 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,10 +109,12 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject +#if defined(PYBIND11_CPP20) namespace typevar { typedef py::typing::TypeVar<"T"> TypeVarT; typedef py::typing::TypeVar<"V"> TypeVarV; } // namespace typevar +#endif TEST_SUBMODULE(pytypes, m) { m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); }); @@ -854,7 +856,13 @@ TEST_SUBMODULE(pytypes, m) { [](const py::typing::List &l) -> py::typing::List { return l; }); - m.def("annotate_listT_to_T", - [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); - m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); + + #if defined(PYBIND11_CPP20) + m.def("annotate_listT_to_T", + [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); + m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); + #else + m.def("annotate_listT_to_T", "annotate_listT_to_T(arg0: list[T]) -> T"); + m.def("annotate_object_to_T", "annotate_object_to_T(arg0: object) -> T"); + #endif } From 3cc5a14dd2711b9c8f63593a0a7de8c55a6459df Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:23:01 +0000 Subject: [PATCH 22/50] style: pre-commit fixes --- tests/test_pytypes.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 245197172a..c988b24416 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -857,12 +857,12 @@ TEST_SUBMODULE(pytypes, m) { return l; }); - #if defined(PYBIND11_CPP20) - m.def("annotate_listT_to_T", - [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); - m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); - #else - m.def("annotate_listT_to_T", "annotate_listT_to_T(arg0: list[T]) -> T"); - m.def("annotate_object_to_T", "annotate_object_to_T(arg0: object) -> T"); - #endif +#if defined(PYBIND11_CPP20) + m.def("annotate_listT_to_T", + [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); + m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); +#else + m.def("annotate_listT_to_T", "annotate_listT_to_T(arg0: list[T]) -> T"); + m.def("annotate_object_to_T", "annotate_object_to_T(arg0: object) -> T"); +#endif } From 34ecc43ffce0b9f17c61205a16d02de99abb78da Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 15:24:51 -0400 Subject: [PATCH 23/50] add missing cpp20++ check --- tests/test_pytypes.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index c988b24416..affcc1d4f3 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -110,10 +110,10 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject #if defined(PYBIND11_CPP20) -namespace typevar { -typedef py::typing::TypeVar<"T"> TypeVarT; -typedef py::typing::TypeVar<"V"> TypeVarV; -} // namespace typevar + namespace typevar { + typedef py::typing::TypeVar<"T"> TypeVarT; + typedef py::typing::TypeVar<"V"> TypeVarV; + } // namespace typevar #endif TEST_SUBMODULE(pytypes, m) { @@ -852,16 +852,18 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); + +#if defined(PYBIND11_CPP20) m.def("annotate_generic_containers", [](const py::typing::List &l) -> py::typing::List { return l; }); -#if defined(PYBIND11_CPP20) m.def("annotate_listT_to_T", [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); #else + m.def("annotate_generic_containers", "annotate_generic_containers(arg0: list[T]) -> list[V]"); m.def("annotate_listT_to_T", "annotate_listT_to_T(arg0: list[T]) -> T"); m.def("annotate_object_to_T", "annotate_object_to_T(arg0: object) -> T"); #endif From dd8d648509c16e1b42d8eceb2192b002b880ec91 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:25:21 +0000 Subject: [PATCH 24/50] style: pre-commit fixes --- tests/test_pytypes.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index affcc1d4f3..b189ceb5f4 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -110,10 +110,10 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject #if defined(PYBIND11_CPP20) - namespace typevar { - typedef py::typing::TypeVar<"T"> TypeVarT; - typedef py::typing::TypeVar<"V"> TypeVarV; - } // namespace typevar +namespace typevar { +typedef py::typing::TypeVar<"T"> TypeVarT; +typedef py::typing::TypeVar<"V"> TypeVarV; +} // namespace typevar #endif TEST_SUBMODULE(pytypes, m) { @@ -852,7 +852,6 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); - #if defined(PYBIND11_CPP20) m.def("annotate_generic_containers", [](const py::typing::List &l) -> py::typing::List { From e98b6a02c40a2f9990d6a2b5c957809d044af5d5 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 15:41:30 -0400 Subject: [PATCH 25/50] Add C++20 check to python --- tests/test_pytypes.cpp | 4 ---- tests/test_pytypes.py | 10 ++++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index b189ceb5f4..f0a2325338 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -861,9 +861,5 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_listT_to_T", [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); -#else - m.def("annotate_generic_containers", "annotate_generic_containers(arg0: list[T]) -> list[V]"); - m.def("annotate_listT_to_T", "annotate_listT_to_T(arg0: list[T]) -> T"); - m.def("annotate_object_to_T", "annotate_object_to_T(arg0: object) -> T"); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 6318090bdd..7ca7b01679 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -7,6 +7,7 @@ import env from pybind11_tests import detailed_error_messages_enabled from pybind11_tests import pytypes as m +from pybind11_test import cpp_std def test_obj_class_name(): @@ -958,6 +959,9 @@ def test_fn_annotations(doc): def test_fn_generics(doc): + if cpp_std() != "C++20"{ + return + } assert ( doc(m.annotate_generic_containers) == "annotate_generic_containers(arg0: list[T]) -> list[V]" @@ -965,8 +969,14 @@ def test_fn_generics(doc): def test_fn_generics_uniqueness(doc): + if cpp_std() != "C++20"{ + return + } assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" def test_object_and_typevar_equilvance(doc): + if cpp_std() != "C++20"{ + return + } assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From 41225059f65f2fb3abf0fbbfe71e483a3c3642e0 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 15:45:39 -0400 Subject: [PATCH 26/50] Fix python if { --- tests/test_pytypes.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 7ca7b01679..af7198c756 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -959,9 +959,9 @@ def test_fn_annotations(doc): def test_fn_generics(doc): - if cpp_std() != "C++20"{ + if cpp_std() != "C++20": return - } + assert ( doc(m.annotate_generic_containers) == "annotate_generic_containers(arg0: list[T]) -> list[V]" @@ -969,14 +969,14 @@ def test_fn_generics(doc): def test_fn_generics_uniqueness(doc): - if cpp_std() != "C++20"{ + if cpp_std() != "C++20": return - } + assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" def test_object_and_typevar_equilvance(doc): - if cpp_std() != "C++20"{ + if cpp_std() != "C++20": return - } + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From c66ebd7e94f4f76d6f3f3d1c27c1607d74aff866 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:46:19 +0000 Subject: [PATCH 27/50] style: pre-commit fixes --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index af7198c756..dc5496cf6e 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -3,11 +3,11 @@ import types import pytest +from pybind11_test import cpp_std import env from pybind11_tests import detailed_error_messages_enabled from pybind11_tests import pytypes as m -from pybind11_test import cpp_std def test_obj_class_name(): From 9954c18596a7a101e3e60cccda477adfd55c1840 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 15:47:59 -0400 Subject: [PATCH 28/50] update test name --- tests/test_pytypes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index af7198c756..87f8829482 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -7,7 +7,7 @@ import env from pybind11_tests import detailed_error_messages_enabled from pybind11_tests import pytypes as m -from pybind11_test import cpp_std +from pybind11_tests import cpp_std def test_obj_class_name(): @@ -958,7 +958,7 @@ def test_fn_annotations(doc): ) -def test_fn_generics(doc): +def test_generics_compatability(doc): if cpp_std() != "C++20": return @@ -968,14 +968,14 @@ def test_fn_generics(doc): ) -def test_fn_generics_uniqueness(doc): +def test_get_generic_from_container(doc): if cpp_std() != "C++20": return assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" -def test_object_and_typevar_equilvance(doc): +def test_object_and_typevar_equivalence(doc): if cpp_std() != "C++20": return From bd954e65715d44d41bda355a3e92a8614cb333e5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:49:09 +0000 Subject: [PATCH 29/50] style: pre-commit fixes --- tests/test_pytypes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 87f8829482..0639907a7f 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -5,9 +5,8 @@ import pytest import env -from pybind11_tests import detailed_error_messages_enabled +from pybind11_tests import cpp_std, detailed_error_messages_enabled from pybind11_tests import pytypes as m -from pybind11_tests import cpp_std def test_obj_class_name(): From 8675c5c52ea516606f33b1e7d62e1577e7388ab4 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 15:53:26 -0400 Subject: [PATCH 30/50] remove call on cpp_std --- tests/test_pytypes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 0639907a7f..aa623bc3d1 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,7 +958,7 @@ def test_fn_annotations(doc): def test_generics_compatability(doc): - if cpp_std() != "C++20": + if cpp_std != "C++20": return assert ( @@ -968,14 +968,14 @@ def test_generics_compatability(doc): def test_get_generic_from_container(doc): - if cpp_std() != "C++20": + if cpp_std != "C++20": return assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" def test_object_and_typevar_equivalence(doc): - if cpp_std() != "C++20": + if cpp_std != "C++20": return assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From 9cd1b16c89bc287d7ed07ac6d319e15ee1a0bd89 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:05:30 -0400 Subject: [PATCH 31/50] make field const --- include/pybind11/typing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index d363d91567..6cf02f47ae 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -68,7 +68,7 @@ template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } - char value[N]; + const char value[N]; }; template From 4e3821dc42e1516b3201d3d9aaf4d98568ce6406 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:16:16 -0400 Subject: [PATCH 32/50] test nontype_template --- include/pybind11/typing.h | 4 ++-- tests/test_pytypes.cpp | 6 ++++-- tests/test_pytypes.py | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 6cf02f47ae..320c1bc0b1 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -#if defined(PYBIND11_CPP20) +#if defined(__cpp_nontype_template_args) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } @@ -136,7 +136,7 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -#if defined(PYBIND11_CPP20) +#if defined(__cpp_nontype_template_args) template struct handle_type_name> { static constexpr auto name = const_name(lit.value); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index f0a2325338..e40ed9bdc0 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,7 +109,7 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject -#if defined(PYBIND11_CPP20) +#if defined(__cpp_nontype_template_args) namespace typevar { typedef py::typing::TypeVar<"T"> TypeVarT; typedef py::typing::TypeVar<"V"> TypeVarV; @@ -852,7 +852,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); -#if defined(PYBIND11_CPP20) +#if defined(__cpp_nontype_template_args) m.def("annotate_generic_containers", [](const py::typing::List &l) -> py::typing::List { return l; @@ -861,5 +861,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_listT_to_T", [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); +#else + m.def("requires_ccp_not_type_template_args", []()); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index aa623bc3d1..bddbe09ff6 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -5,7 +5,7 @@ import pytest import env -from pybind11_tests import cpp_std, detailed_error_messages_enabled +from pybind11_tests import detailed_error_messages_enabled from pybind11_tests import pytypes as m @@ -958,7 +958,7 @@ def test_fn_annotations(doc): def test_generics_compatability(doc): - if cpp_std != "C++20": + if not m.requires_ccp_not_type_template_args: return assert ( @@ -968,14 +968,14 @@ def test_generics_compatability(doc): def test_get_generic_from_container(doc): - if cpp_std != "C++20": + if not m.requires_ccp_not_type_template_args: return assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" def test_object_and_typevar_equivalence(doc): - if cpp_std != "C++20": + if not m.requires_ccp_not_type_template_args: return assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From dd3d0ccdfeb869b046f33fdfaa78826f317556ba Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:21:17 -0400 Subject: [PATCH 33/50] update feature check --- include/pybind11/typing.h | 4 ++-- tests/test_pytypes.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 320c1bc0b1..c18ccc5ccd 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,7 +63,7 @@ class Callable : public function { using function::function; }; -#if defined(__cpp_nontype_template_args) +#if defined(__cpp_nontype_template_parameter_class) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } @@ -136,7 +136,7 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -#if defined(__cpp_nontype_template_args) +#if defined(__cpp_nontype_template_parameter_class) template struct handle_type_name> { static constexpr auto name = const_name(lit.value); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index e40ed9bdc0..03af129594 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,7 +109,7 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject -#if defined(__cpp_nontype_template_args) +#if defined(__cpp_nontype_template_parameter_class) namespace typevar { typedef py::typing::TypeVar<"T"> TypeVarT; typedef py::typing::TypeVar<"V"> TypeVarV; @@ -852,7 +852,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); -#if defined(__cpp_nontype_template_args) +#if defined(__cpp_nontype_template_parameter_class) m.def("annotate_generic_containers", [](const py::typing::List &l) -> py::typing::List { return l; From 0a34ddb0e0324a75a38d1100259ea7f1d372856b Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:23:04 -0400 Subject: [PATCH 34/50] update name of guard --- tests/test_pytypes.cpp | 2 +- tests/test_pytypes.py | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 03af129594..2840f8a870 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -862,6 +862,6 @@ TEST_SUBMODULE(pytypes, m) { [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); #else - m.def("requires_ccp_not_type_template_args", []()); + m.def("requires__cpp_nontype_template_parameter_class", []()); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index bddbe09ff6..d04f1ee012 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -957,9 +957,12 @@ def test_fn_annotations(doc): ) -def test_generics_compatability(doc): - if not m.requires_ccp_not_type_template_args: +def test_generics_compatibility(doc): + try: + m.requires__cpp_nontype_template_parameter_class return + except: + pass assert ( doc(m.annotate_generic_containers) @@ -968,14 +971,20 @@ def test_generics_compatability(doc): def test_get_generic_from_container(doc): - if not m.requires_ccp_not_type_template_args: + try: + m.requires__cpp_nontype_template_parameter_class return + except: + pass assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" def test_object_and_typevar_equivalence(doc): - if not m.requires_ccp_not_type_template_args: + try: + m.requires__cpp_nontype_template_parameter_class return + except: + pass assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From c572871041f8c0913deb0af058a965afaac43001 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:23:41 -0400 Subject: [PATCH 35/50] fix try except in test --- tests/test_pytypes.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index d04f1ee012..45c55167ce 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -960,9 +960,8 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): try: m.requires__cpp_nontype_template_parameter_class - return except: - pass + return assert ( doc(m.annotate_generic_containers) @@ -973,9 +972,8 @@ def test_generics_compatibility(doc): def test_get_generic_from_container(doc): try: m.requires__cpp_nontype_template_parameter_class - return except: - pass + return assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" @@ -983,8 +981,7 @@ def test_get_generic_from_container(doc): def test_object_and_typevar_equivalence(doc): try: m.requires__cpp_nontype_template_parameter_class - return except: - pass + return assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From 676e3bd17da8808c37f49942e8db50dafdd891ac Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:30:28 -0400 Subject: [PATCH 36/50] fix pre commit --- tests/test_pytypes.cpp | 2 +- tests/test_pytypes.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 2840f8a870..3e3a30a3c0 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -862,6 +862,6 @@ TEST_SUBMODULE(pytypes, m) { [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); #else - m.def("requires__cpp_nontype_template_parameter_class", []()); + m.def("requires__cpp_nontype_template_parameter_class", []() {};); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 45c55167ce..0b5dc6017e 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -959,8 +959,8 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): try: - m.requires__cpp_nontype_template_parameter_class - except: + m.requires__cpp_nontype_template_parameter_class() + except NameError: return assert ( @@ -971,8 +971,8 @@ def test_generics_compatibility(doc): def test_get_generic_from_container(doc): try: - m.requires__cpp_nontype_template_parameter_class - except: + m.requires__cpp_nontype_template_parameter_class() + except NameError: return assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" @@ -980,8 +980,8 @@ def test_get_generic_from_container(doc): def test_object_and_typevar_equivalence(doc): try: - m.requires__cpp_nontype_template_parameter_class - except: + m.requires__cpp_nontype_template_parameter_class() + except NameError: return assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From 5dd1d3ba0d11530b4668fbdf7468b970c5cfaf86 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:35:11 -0400 Subject: [PATCH 37/50] remove extra semi colon --- tests/test_pytypes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 3e3a30a3c0..c4c56f1b8a 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -862,6 +862,6 @@ TEST_SUBMODULE(pytypes, m) { [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); #else - m.def("requires__cpp_nontype_template_parameter_class", []() {};); + m.def("requires__cpp_nontype_template_parameter_class", []() {}); #endif } From 98c115daea8ef8550f8f0a8684e915ef45fc8e6a Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:40:21 -0400 Subject: [PATCH 38/50] except AttributeError --- tests/test_pytypes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 0b5dc6017e..979eb4b457 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -960,7 +960,7 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): try: m.requires__cpp_nontype_template_parameter_class() - except NameError: + except AttributeError: return assert ( @@ -972,7 +972,7 @@ def test_generics_compatibility(doc): def test_get_generic_from_container(doc): try: m.requires__cpp_nontype_template_parameter_class() - except NameError: + except AttributeError: return assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" @@ -981,7 +981,7 @@ def test_get_generic_from_container(doc): def test_object_and_typevar_equivalence(doc): try: m.requires__cpp_nontype_template_parameter_class() - except NameError: + except AttributeError: return assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From 1dab40f279c27376c26e2695f6c978dd3c9d121c Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 18:58:44 -0400 Subject: [PATCH 39/50] fix try except in test --- tests/test_pytypes.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 979eb4b457..81528635e6 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -960,8 +960,9 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): try: m.requires__cpp_nontype_template_parameter_class() - except AttributeError: return + except AttributeError: + pass assert ( doc(m.annotate_generic_containers) @@ -972,8 +973,9 @@ def test_generics_compatibility(doc): def test_get_generic_from_container(doc): try: m.requires__cpp_nontype_template_parameter_class() - except AttributeError: return + except AttributeError: + pass assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" @@ -981,7 +983,8 @@ def test_get_generic_from_container(doc): def test_object_and_typevar_equivalence(doc): try: m.requires__cpp_nontype_template_parameter_class() - except AttributeError: return + except AttributeError: + pass assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From 39516fd4790ce4b4ad9c1e414ff0675ba087cae5 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 19:04:08 -0400 Subject: [PATCH 40/50] remove const --- include/pybind11/typing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index c18ccc5ccd..de362cf5b8 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -68,7 +68,7 @@ template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } - const char value[N]; + char value[N]; }; template From 237136aff6fe055ce39c294011e093e64056ace2 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 20:56:38 -0400 Subject: [PATCH 41/50] Clean up tests --- tests/test_pytypes.cpp | 2 -- tests/test_pytypes.py | 21 ++++++--------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index c4c56f1b8a..3ad4b7c7ba 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -861,7 +861,5 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_listT_to_T", [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); -#else - m.def("requires__cpp_nontype_template_parameter_class", []() {}); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 81528635e6..ff1128a32f 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -959,32 +959,23 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): try: - m.requires__cpp_nontype_template_parameter_class() - return - except AttributeError: - pass - - assert ( + assert ( doc(m.annotate_generic_containers) == "annotate_generic_containers(arg0: list[T]) -> list[V]" ) + except AttributeError: + pass def test_get_generic_from_container(doc): try: - m.requires__cpp_nontype_template_parameter_class() - return + assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" except AttributeError: pass - - assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" - + def test_object_and_typevar_equivalence(doc): try: - m.requires__cpp_nontype_template_parameter_class() - return + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" except AttributeError: pass - - assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From d13e3d515b8f6393e36925a269bcf3118f46339b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Jun 2024 00:57:02 +0000 Subject: [PATCH 42/50] style: pre-commit fixes --- tests/test_pytypes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index ff1128a32f..cb7b1d68cd 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -960,9 +960,9 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): try: assert ( - doc(m.annotate_generic_containers) - == "annotate_generic_containers(arg0: list[T]) -> list[V]" - ) + doc(m.annotate_generic_containers) + == "annotate_generic_containers(arg0: list[T]) -> list[V]" + ) except AttributeError: pass @@ -972,7 +972,7 @@ def test_get_generic_from_container(doc): assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" except AttributeError: pass - + def test_object_and_typevar_equivalence(doc): try: From a6c5676173e7ba573d53ef73d145f88ab4adeec1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 21:10:47 -0400 Subject: [PATCH 43/50] use contextlib.suppres --- tests/test_pytypes.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index cb7b1d68cd..e9fd5501b2 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,24 +958,18 @@ def test_fn_annotations(doc): def test_generics_compatibility(doc): - try: + with contextlib.suppress(AttributeError): assert ( doc(m.annotate_generic_containers) == "annotate_generic_containers(arg0: list[T]) -> list[V]" ) - except AttributeError: - pass def test_get_generic_from_container(doc): - try: + with contextlib.suppress(AttributeError): assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" - except AttributeError: - pass def test_object_and_typevar_equivalence(doc): - try: + with contextlib.suppress(AttributeError): assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" - except AttributeError: - pass From ca868f8e4420b9a4a52522796e640844ee1a7bbc Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 10:44:39 -0400 Subject: [PATCH 44/50] request changes --- include/pybind11/typing.h | 8 ++++---- tests/test_pytypes.cpp | 3 +++ tests/test_pytypes.py | 25 +++++++++++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 9bab8ac300..e1c47c0739 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -71,7 +71,7 @@ struct StringLiteral { char value[N]; }; -template +template class TypeVar : public object { PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) using object::object; @@ -146,9 +146,9 @@ struct handle_type_name> { }; #if defined(__cpp_nontype_template_parameter_class) -template -struct handle_type_name> { - static constexpr auto name = const_name(lit.value); +template +struct handle_type_name> { + static constexpr auto name = const_name(StrLit.value); }; #endif template diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 5bf0b10dc8..375e218acc 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -861,6 +861,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_listT_to_T", [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); + m.attr("if_defined__cpp_nontype_template_parameter_class") = true; +#else + m.attr("if_defined__cpp_nontype_template_parameter_class") = false; #endif m.def("annotate_union", [](py::typing::List> l, diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 5e69c41d3d..e8dc2396b6 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -957,22 +957,19 @@ def test_fn_annotations(doc): ) -def test_generics_compatibility(doc): - with contextlib.suppress(AttributeError): - assert ( - doc(m.annotate_generic_containers) - == "annotate_generic_containers(arg0: list[T]) -> list[V]" - ) - - -def test_get_generic_from_container(doc): - with contextlib.suppress(AttributeError): - assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" +@pytest.mark.skipif( + not m.if_defined__cpp_nontype_template_parameter_class, + reason="C++20 feature not available.", +) +def test_typevar(doc): + assert ( + doc(m.annotate_generic_containers) + == "annotate_generic_containers(arg0: list[T]) -> list[V]" + ) + assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" -def test_object_and_typevar_equivalence(doc): - with contextlib.suppress(AttributeError): - assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" def test_union_annotations(doc): From 32fde8e2584e9932b1f82f054ec644acaac8801f Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 10:46:29 -0400 Subject: [PATCH 45/50] lint --- tests/test_pytypes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1f739520b2..dd7d8e5b92 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -970,6 +970,8 @@ def test_typevar(doc): assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" + + def test_type_annotation(doc): assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> None" From 318ec6d4fd11b895d127f10bb95f698e7caf21ea Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 11:33:06 -0400 Subject: [PATCH 46/50] Add comments --- include/pybind11/typing.h | 50 +++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 74c826ea86..eb8a1073d8 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,20 +63,6 @@ class Callable : public function { using function::function; }; -#if defined(__cpp_nontype_template_parameter_class) -template -struct StringLiteral { - constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } - - char value[N]; -}; - -template -class TypeVar : public object { - PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) - using object::object; -}; -#endif template class Type : public type { using type::type; @@ -92,6 +78,29 @@ class Optional : public object { using object::object; }; +#if defined(__cpp_nontype_template_parameter_class) +template +struct StringLiteral { +// This struct is different than detail::descr since it accepts a string rather than a type. +// StringLiteral is used in TypeVar to create "instances" of c++ type objects. + constexpr StringLiteral(const char (&str)[N]) { + // Ensures This copy is done at compile time + if (std::is_constant_evaluated()) { + std::copy_n(str, N, value); + } + } + char value[N]; +}; + +// Example syntax for creating a TypeVar. +// typedef typing::TypeVar<"T"> TypeVarT; +template +class TypeVar : public object { + PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) + using object::object; +}; +#endif + PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) @@ -150,12 +159,6 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; -#if defined(__cpp_nontype_template_parameter_class) -template -struct handle_type_name> { - static constexpr auto name = const_name(StrLit.value); -}; -#endif template struct handle_type_name> { static constexpr auto name = const_name("type[") + make_caster::name + const_name("]"); @@ -173,5 +176,12 @@ struct handle_type_name> { static constexpr auto name = const_name("Optional[") + make_caster::name + const_name("]"); }; +#if defined(__cpp_nontype_template_parameter_class) +template +struct handle_type_name> { + static constexpr auto name = const_name(StrLit.value); +}; +#endif + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) From e9f7889dcba3f1c614ff52eb9f7a76f99721f24d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Jun 2024 15:33:28 +0000 Subject: [PATCH 47/50] style: pre-commit fixes --- include/pybind11/typing.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index eb8a1073d8..10c3274153 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -81,8 +81,8 @@ class Optional : public object { #if defined(__cpp_nontype_template_parameter_class) template struct StringLiteral { -// This struct is different than detail::descr since it accepts a string rather than a type. -// StringLiteral is used in TypeVar to create "instances" of c++ type objects. + // This struct is different than detail::descr since it accepts a string rather than a type. + // StringLiteral is used in TypeVar to create "instances" of c++ type objects. constexpr StringLiteral(const char (&str)[N]) { // Ensures This copy is done at compile time if (std::is_constant_evaluated()) { From 648c5a05397bd6e4042d0f406561419b700b2d87 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 12:01:36 -0400 Subject: [PATCH 48/50] Add support for unions and optionals to be compatible with object --- include/pybind11/typing.h | 4 +++- tests/test_pytypes.cpp | 39 +++++++++++++++++++++------------ tests/test_pytypes.py | 45 +++++++++++++++++++++++++-------------- 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 10c3274153..5b9498422d 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -70,11 +70,13 @@ class Type : public type { template class Union : public object { + PYBIND11_OBJECT_DEFAULT(Union, object, PyObject_Type) using object::object; }; template class Optional : public object { + PYBIND11_OBJECT_DEFAULT(Optional, object, PyObject_Type) using object::object; }; @@ -95,7 +97,7 @@ struct StringLiteral { // Example syntax for creating a TypeVar. // typedef typing::TypeVar<"T"> TypeVarT; template -class TypeVar : public object { +class TypeVar : public object{ PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) using object::object; }; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 0cd0ed24db..1b7a5043db 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -851,21 +851,8 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_iterator_int", [](const py::typing::Iterator &) {}); m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); - m.def("annotate_type", [](const py::typing::Type &) {}); + m.def("annotate_type", [](const py::typing::Type &t) -> py::type { return t;}); -#if defined(__cpp_nontype_template_parameter_class) - m.def("annotate_generic_containers", - [](const py::typing::List &l) -> py::typing::List { - return l; - }); - - m.def("annotate_listT_to_T", - [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); - m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); - m.attr("if_defined__cpp_nontype_template_parameter_class") = true; -#else - m.attr("if_defined__cpp_nontype_template_parameter_class") = false; -#endif m.def("annotate_union", [](py::typing::List> l, py::str a, @@ -881,10 +868,34 @@ TEST_SUBMODULE(pytypes, m) { [](py::typing::List> &l) -> py::typing::List> { return l; }); + m.def("annotate_union_to_object", + [](py::typing::Union &o) -> py::object { + return o; + }); + m.def("annotate_optional", [](py::list &list) -> py::typing::List> { list.append(py::str("hi")); list.append(py::none()); return list; }); + m.def("annotate_optional_to_object", + [](py::typing::Optional &o) -> py::object { + return o; + }); + + +#if defined(__cpp_nontype_template_parameter_class) + m.def("annotate_generic_containers", + [](const py::typing::List &l) -> py::typing::List { + return l; + }); + + m.def("annotate_listT_to_T", + [](const py::typing::List &l) -> typevar::TypeVarT { return l[0]; }); + m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); + m.attr("if_defined__cpp_nontype_template_parameter_class") = true; +#else + m.attr("if_defined__cpp_nontype_template_parameter_class") = false; +#endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index dd7d8e5b92..ba6e842ff3 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -957,23 +957,8 @@ def test_fn_annotations(doc): ) -@pytest.mark.skipif( - not m.if_defined__cpp_nontype_template_parameter_class, - reason="C++20 feature not available.", -) -def test_typevar(doc): - assert ( - doc(m.annotate_generic_containers) - == "annotate_generic_containers(arg0: list[T]) -> list[V]" - ) - - assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" - - assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" - - def test_type_annotation(doc): - assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> None" + assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> type" def test_union_annotations(doc): @@ -989,9 +974,37 @@ def test_union_typing_only(doc): == "union_typing_only(arg0: list[Union[str]]) -> list[Union[int]]" ) +def test_union_object_annotations(doc): + assert ( + doc(m.annotate_union_to_object) + == "annotate_union_to_object(arg0: Union[int, str]) -> object" + ) + def test_optional_annotations(doc): assert ( doc(m.annotate_optional) == "annotate_optional(arg0: list) -> list[Optional[str]]" ) + + +def test_optional_object_annotations(doc): + assert ( + doc(m.annotate_optional_to_object) + == "annotate_optional_to_object(arg0: Optional[int]) -> object" + ) + + +@pytest.mark.skipif( + not m.if_defined__cpp_nontype_template_parameter_class, + reason="C++20 feature not available.", +) +def test_typevar(doc): + assert ( + doc(m.annotate_generic_containers) + == "annotate_generic_containers(arg0: list[T]) -> list[V]" + ) + + assert doc(m.annotate_listT_to_T) == "annotate_listT_to_T(arg0: list[T]) -> T" + + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" From c452d2f2a13b106c1572e55da88648d099dda9f9 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 12:02:18 -0400 Subject: [PATCH 49/50] lint --- include/pybind11/typing.h | 2 +- tests/test_pytypes.cpp | 11 +++-------- tests/test_pytypes.py | 1 + 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 5b9498422d..7887470ae5 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -97,7 +97,7 @@ struct StringLiteral { // Example syntax for creating a TypeVar. // typedef typing::TypeVar<"T"> TypeVarT; template -class TypeVar : public object{ +class TypeVar : public object { PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) using object::object; }; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 1b7a5043db..ce003e0926 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -851,7 +851,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_iterator_int", [](const py::typing::Iterator &) {}); m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); - m.def("annotate_type", [](const py::typing::Type &t) -> py::type { return t;}); + m.def("annotate_type", [](const py::typing::Type &t) -> py::type { return t; }); m.def("annotate_union", [](py::typing::List> l, @@ -869,9 +869,7 @@ TEST_SUBMODULE(pytypes, m) { -> py::typing::List> { return l; }); m.def("annotate_union_to_object", - [](py::typing::Union &o) -> py::object { - return o; - }); + [](py::typing::Union &o) -> py::object { return o; }); m.def("annotate_optional", [](py::list &list) -> py::typing::List> { @@ -880,10 +878,7 @@ TEST_SUBMODULE(pytypes, m) { return list; }); m.def("annotate_optional_to_object", - [](py::typing::Optional &o) -> py::object { - return o; - }); - + [](py::typing::Optional &o) -> py::object { return o; }); #if defined(__cpp_nontype_template_parameter_class) m.def("annotate_generic_containers", diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index ba6e842ff3..a5cb6edf13 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -974,6 +974,7 @@ def test_union_typing_only(doc): == "union_typing_only(arg0: list[Union[str]]) -> list[Union[int]]" ) + def test_union_object_annotations(doc): assert ( doc(m.annotate_union_to_object) From 7b83557049e366295da970e3d787d82b6270373a Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 12:04:31 -0400 Subject: [PATCH 50/50] remove comment --- include/pybind11/typing.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 7887470ae5..8d91c51d90 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -83,14 +83,7 @@ class Optional : public object { #if defined(__cpp_nontype_template_parameter_class) template struct StringLiteral { - // This struct is different than detail::descr since it accepts a string rather than a type. - // StringLiteral is used in TypeVar to create "instances" of c++ type objects. - constexpr StringLiteral(const char (&str)[N]) { - // Ensures This copy is done at compile time - if (std::is_constant_evaluated()) { - std::copy_n(str, N, value); - } - } + constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } char value[N]; };