From d872367cfdb1cd3d183a7e6341dfa00cd10f5fb1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Thu, 13 Jun 2024 23:13:53 -0400 Subject: [PATCH 01/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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/68] 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 10739e6455e3c886686f5318f9f0592e7f6bc595 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 20:58:13 -0400 Subject: [PATCH 43/68] start string literal --- include/pybind11/typing.h | 17 +++++++++++++++++ tests/test_pytypes.cpp | 6 ++++++ tests/test_pytypes.py | 9 ++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index de362cf5b8..c15e7f9d88 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -76,6 +76,16 @@ class TypeVar : public object { PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) using object::object; }; + +template +class Literal : public py::str { + using str::str; +}; + +// template +// class Literal : public py::int { +// using str::str; +// }; #endif PYBIND11_NAMESPACE_END(typing) @@ -141,6 +151,13 @@ template struct handle_type_name> { static constexpr auto name = const_name(lit.value); }; + +template +struct handle_type_name> { + static constexpr auto name = const_name("Literal[")+ + pybind11::detail::concat(lit.value); + + const_name("]"); +}; #endif PYBIND11_NAMESPACE_END(detail) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 3ad4b7c7ba..5aec57265c 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -861,5 +861,11 @@ 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; }); + + typedef typing::Literal<"A", "B", "C"> stringLiteral; + + m.def("annotate_str_literal", [](const stringLiteral &) {}); +#else + m.def("requires__cpp_nontype_template_parameter_class", []() {}); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index ff1128a32f..0b79fa9a92 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -972,10 +972,17 @@ 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: assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" except AttributeError: pass + + +def test_string_literal(doc): + try: + assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" + except AttributeError: + pass From 311fa83265d88686eb2e17c81d17e3e06f4ee35f Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 21:05:47 -0400 Subject: [PATCH 44/68] start int literal --- include/pybind11/typing.h | 15 +++++++++++---- tests/test_pytypes.cpp | 6 ++---- tests/test_pytypes.py | 8 +++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index c15e7f9d88..872c33e011 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -82,10 +82,10 @@ class Literal : public py::str { using str::str; }; -// template -// class Literal : public py::int { -// using str::str; -// }; +template +class Literal : public py::int_ { + using int_::int_; +}; #endif PYBIND11_NAMESPACE_END(typing) @@ -158,6 +158,13 @@ struct handle_type_name> { pybind11::detail::concat(lit.value); + const_name("]"); }; + +template +struct handle_type_name> { + static constexpr auto name = const_name("Literal[")+ + pybind11::detail::concat(intLit); + + const_name("]"); +}; #endif PYBIND11_NAMESPACE_END(detail) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 5aec57265c..31c62bd2ce 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -863,9 +863,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); typedef typing::Literal<"A", "B", "C"> stringLiteral; - - m.def("annotate_str_literal", [](const stringLiteral &) {}); -#else - m.def("requires__cpp_nontype_template_parameter_class", []() {}); + m.def("annotate_str_literal", [](const stringLiteral s) { return s;}); + typedef typing::Literal<1, 2, 3> intLiteral; #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index c1b4c35aa8..0db422efba 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -983,6 +983,12 @@ def test_object_and_typevar_equivalence(doc): def test_string_literal(doc): try: - assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" + assert doc(m.annotate_str_literal) == 'annotate_object_to_T(arg0: Literal["A", "B", C"]) -> str' + except AttributeError: + pass + +def test_int_literal(doc): + try: + assert doc(m.annotate_int_literal) == 'annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int' except AttributeError: pass From 7c3c09eaa026fbba2129f4c230d1bbaa48e57bdb Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 21:05:56 -0400 Subject: [PATCH 45/68] func declare --- tests/test_pytypes.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 31c62bd2ce..ad4ff5b2e6 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -865,5 +865,7 @@ TEST_SUBMODULE(pytypes, m) { typedef typing::Literal<"A", "B", "C"> stringLiteral; m.def("annotate_str_literal", [](const stringLiteral s) { return s;}); typedef typing::Literal<1, 2, 3> intLiteral; + m.def("annotate_int_literal", [](const intLiteral i) { return i;}); + #endif } From 905638a5175728ec5d4ebc0d8ebb909e0d0e6a05 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 21:08:45 -0400 Subject: [PATCH 46/68] commit clean --- tests/test_pytypes.cpp | 6 +++--- tests/test_pytypes.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index ad4ff5b2e6..2529133020 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -863,9 +863,9 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); typedef typing::Literal<"A", "B", "C"> stringLiteral; - m.def("annotate_str_literal", [](const stringLiteral s) { return s;}); + m.def("annotate_str_literal", [](const stringLiteral s) { return s; }); typedef typing::Literal<1, 2, 3> intLiteral; - m.def("annotate_int_literal", [](const intLiteral i) { return i;}); - + m.def("annotate_int_literal", [](const intLiteral i) { return i; }); + #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 0db422efba..7992f68e3c 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -973,7 +973,7 @@ def test_get_generic_from_container(doc): except AttributeError: pass - + def test_object_and_typevar_equivalence(doc): try: assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" @@ -983,12 +983,19 @@ def test_object_and_typevar_equivalence(doc): def test_string_literal(doc): try: - assert doc(m.annotate_str_literal) == 'annotate_object_to_T(arg0: Literal["A", "B", C"]) -> str' + assert ( + doc(m.annotate_str_literal) + == 'annotate_object_to_T(arg0: Literal["A", "B", C"]) -> str' + ) except AttributeError: pass + def test_int_literal(doc): try: - assert doc(m.annotate_int_literal) == 'annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int' + assert ( + doc(m.annotate_int_literal) + == "annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int" + ) except AttributeError: pass From a6c5676173e7ba573d53ef73d145f88ab4adeec1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 21:10:47 -0400 Subject: [PATCH 47/68] 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 c04569564a51bab0b11c24f2b8219ce5bfb954d8 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 14 Jun 2024 21:13:06 -0400 Subject: [PATCH 48/68] resolve stash --- tests/test_pytypes.cpp | 1 - tests/test_pytypes.py | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 2529133020..8937f57a07 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -866,6 +866,5 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_str_literal", [](const stringLiteral s) { return s; }); typedef typing::Literal<1, 2, 3> intLiteral; m.def("annotate_int_literal", [](const intLiteral i) { return i; }); - #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index db5b96413e..446cb34bf5 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -981,7 +981,6 @@ def test_string_literal(doc): doc(m.annotate_str_literal) == 'annotate_object_to_T(arg0: Literal["A", "B", C"]) -> str' ) - def test_int_literal(doc): From 519e32bb20f5bba533b8b4a8cc2c30da2251327d Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 08:24:19 -0400 Subject: [PATCH 49/68] more literal type --- include/pybind11/typing.h | 55 ++++++++++++++++++++++++++++++++------- tests/test_pytypes.cpp | 10 +++++-- tests/test_pytypes.py | 30 +++++++++++++++++++++ 3 files changed, 83 insertions(+), 12 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 872c33e011..a1fa3213ee 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -77,6 +77,9 @@ class TypeVar : public object { using object::object; }; + +// Does not currently support Literals of byte strings, unicode strings, and Enum values. +// Also due to how C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] template class Literal : public py::str { using str::str; @@ -86,6 +89,23 @@ template class Literal : public py::int_ { using int_::int_; }; + +template +class Literal : public py::bool_ { + using bool_::bool_; +}; + +template +class Literal : public py::none { + using none::none; +}; + +template +class Literal : public py::object { + PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) + using object::object; +}; + #endif PYBIND11_NAMESPACE_END(typing) @@ -153,19 +173,34 @@ struct handle_type_name> { }; template -struct handle_type_name> { - static constexpr auto name = const_name("Literal[")+ - pybind11::detail::concat(lit.value); - + const_name("]"); +struct handle_type_name> { + static constexpr auto name + = const_name("Literal[") + pybind11::detail::concat(lit.value) + const_name("]"); }; template -struct handle_type_name> { - static constexpr auto name = const_name("Literal[")+ - pybind11::detail::concat(intLit); - + const_name("]"); +struct handle_type_name> { + static constexpr auto name + = const_name("Literal[") + pybind11::detail::concat(intLit) + const_name("]"); }; + +template +struct handle_type_name> { + static constexpr auto name + = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); +} + +template <> +struct handle_type_name> { + static constexpr auto name + = const_name("Literal[None]"); +} + +template +struct handle_type_name> { + // TODO handle conststr + static constexpr auto name = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); +} #endif -PYBIND11_NAMESPACE_END(detail) -PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 8937f57a07..937e16933f 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -863,8 +863,14 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_object_to_T", [](const py::object &o) -> typevar::TypeVarT { return o; }); typedef typing::Literal<"A", "B", "C"> stringLiteral; - m.def("annotate_str_literal", [](const stringLiteral s) { return s; }); + m.def("annotate_str_literal", [](const stringLiteral s) -> py::str { return s; }); typedef typing::Literal<1, 2, 3> intLiteral; - m.def("annotate_int_literal", [](const intLiteral i) { return i; }); + m.def("annotate_int_literal", [](const intLiteral i) -> py::int_ { return i; }); + typedef typing::Literal boolLiteral; + m.def("annotate_bool_literal", [](const boolLiteral i) -> py::bool_ { return i; }); + typedef typing::Literal noneLiteral; + m.def("annotate_none_literal", [](const noneLiteral i) -> py::none { return i; }); + typedef typing::Literal<"A", 1, true, py::none> anyLiteral; + m.def("annotate_any_literal", [](const anyLiteral i) -> py::object { return i; }); #endif } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 446cb34bf5..57e434171b 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -989,3 +989,33 @@ def test_int_literal(doc): doc(m.annotate_int_literal) == "annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int" ) + +def test_bool_literal(doc): + with contextlib.suppress(AttributeError): + assert ( + doc(m.annotate_bool_literal) + == "annotate_object_to_T(arg0: Literal[True, False]) -> bool" + ) + + +def test_none_literal(doc): + with contextlib.suppress(AttributeError): + assert ( + doc(m.annotate_none_literal) + == "annotate_object_to_T(arg0: Literal[None]) -> None" + ) + +def test_any_literal(doc): + with contextlib.suppress(AttributeError): + assert ( + doc(m.annotate_none_literal) + == 'annotate_object_to_T(arg0: Literal["A", 1, true, None]) -> object' + ) + + +# def test_literal_order(doc): +# with contextlib.suppress(AttributeError): +# assert ( +# doc(m.annotate_int_literal) +# == "annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int" +# ) \ No newline at end of file From ba3259bf5d2ef79ed2f8c415351365f382184bbf Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 09:07:59 -0400 Subject: [PATCH 50/68] fix annotation name --- tests/test_pytypes.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 57e434171b..5eba81d983 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -979,7 +979,7 @@ def test_string_literal(doc): with contextlib.suppress(AttributeError): assert ( doc(m.annotate_str_literal) - == 'annotate_object_to_T(arg0: Literal["A", "B", C"]) -> str' + == 'annotate_str_literal(arg0: Literal["A", "B", C"]) -> str' ) @@ -987,14 +987,15 @@ def test_int_literal(doc): with contextlib.suppress(AttributeError): assert ( doc(m.annotate_int_literal) - == "annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int" + == "annotate_int_literal(arg0: Literal[1, 2, 3]) -> int" ) + def test_bool_literal(doc): with contextlib.suppress(AttributeError): assert ( doc(m.annotate_bool_literal) - == "annotate_object_to_T(arg0: Literal[True, False]) -> bool" + == "annotate_bool_literal(arg0: Literal[True, False]) -> bool" ) @@ -1002,14 +1003,15 @@ def test_none_literal(doc): with contextlib.suppress(AttributeError): assert ( doc(m.annotate_none_literal) - == "annotate_object_to_T(arg0: Literal[None]) -> None" + == "annotate_none_literal(arg0: Literal[None]) -> None" ) + def test_any_literal(doc): with contextlib.suppress(AttributeError): assert ( - doc(m.annotate_none_literal) - == 'annotate_object_to_T(arg0: Literal["A", 1, true, None]) -> object' + doc(m.annotate_any_literal) + == 'annotate_any_literal(arg0: Literal["A", 1, true, None]) -> object' ) From f087d74e73267f24198d306205abc29f470f41af Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 10:33:42 -0400 Subject: [PATCH 51/68] stash --- include/pybind11/typing.h | 104 ++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 44 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index a1fa3213ee..e5c76ca0f1 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -80,31 +80,47 @@ class TypeVar : public object { // Does not currently support Literals of byte strings, unicode strings, and Enum values. // Also due to how C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] -template -class Literal : public py::str { - using str::str; -}; +// template +// class Literal : public str { +// using str::str; +// }; -template -class Literal : public py::int_ { - using int_::int_; +template +class Literal : public T { + // if std::is_same{ + // PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type); + // } + using T::T; }; -template -class Literal : public py::bool_ { - using bool_::bool_; -}; +template +typedef Literal LiteralStr; -template -class Literal : public py::none { - using none::none; -}; +typedef LiteralStr<"1", "2"> LiteralStrOneTwo; -template -class Literal : public py::object { - PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) - using object::object; -}; + +// typedef Literal LiteralStrThreeFour; + +// template +// class Literal : public py::int_ { +// using int_::int_; +// }; + +// template +// class Literal : public bool_ { +// using bool_::bool_; +// }; + +// template +// class Literal : public none { +// using none::none; +// }; + +// template +// class Literal : public py::object { +// PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) +// using object::object; +// }; #endif @@ -173,34 +189,34 @@ struct handle_type_name> { }; template -struct handle_type_name> { +struct handle_type_name> { static constexpr auto name = const_name("Literal[") + pybind11::detail::concat(lit.value) + const_name("]"); }; -template -struct handle_type_name> { - static constexpr auto name - = const_name("Literal[") + pybind11::detail::concat(intLit) + const_name("]"); -}; - -template -struct handle_type_name> { - static constexpr auto name - = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); -} - -template <> -struct handle_type_name> { - static constexpr auto name - = const_name("Literal[None]"); -} - -template -struct handle_type_name> { - // TODO handle conststr - static constexpr auto name = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); -} +// template +// struct handle_type_name> { +// static constexpr auto name +// = const_name("Literal[") + pybind11::detail::concat(intLit) + const_name("]"); +// }; + +// template +// struct handle_type_name> { +// static constexpr auto name +// = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); +// } + +// template <> +// struct handle_type_name> { +// static constexpr auto name +// = const_name("Literal[None]"); +// } + +// template +// struct handle_type_name> { +// // TODO handle conststr +// static constexpr auto name = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); +// } #endif PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) From ca868f8e4420b9a4a52522796e640844ee1a7bbc Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Sat, 15 Jun 2024 10:44:39 -0400 Subject: [PATCH 52/68] 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 53/68] 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 54/68] 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 55/68] 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 56/68] 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 57/68] 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 58/68] 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]; }; From 4bd435180e67b77fa168aa1374502197b91e0a3b Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Mon, 24 Jun 2024 18:45:53 -0400 Subject: [PATCH 59/68] Create Literal Type implementation --- include/pybind11/typing.h | 120 ++++---------------------------------- tests/test_pytypes.cpp | 47 +++++---------- tests/test_pytypes.py | 76 +----------------------- 3 files changed, 27 insertions(+), 216 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 3078d0cd8e..673adb3ef5 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -63,67 +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; -}; - - -// Does not currently support Literals of byte strings, unicode strings, and Enum values. -// Also due to how C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] -// template -// class Literal : public str { -// using str::str; -// }; - -template -class Literal : public T { - // if std::is_same{ - // PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type); - // } - using T::T; -}; - -template -typedef Literal LiteralStr; - -typedef LiteralStr<"1", "2"> LiteralStrOneTwo; - - -// typedef Literal LiteralStrThreeFour; - -// template -// class Literal : public py::int_ { -// using int_::int_; -// }; - -// template -// class Literal : public bool_ { -// using bool_::bool_; -// }; - -// template -// class Literal : public none { -// using none::none; -// }; - -// template -// class Literal : public py::object { -// PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) -// using object::object; -// }; - -#endif - template class Type : public type { using type::type; @@ -141,18 +80,16 @@ class Optional : public object { using object::object; }; +// Also due to how C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] #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]; + constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } + char name[N]; }; - -// Example syntax for creating a TypeVar. -// typedef typing::TypeVar<"T"> TypeVarT; -template -class TypeVar : public object { - PYBIND11_OBJECT_DEFAULT(TypeVar, object, PyObject_Type) +template +class Literal : public object { + PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) using object::object; }; #endif @@ -215,44 +152,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(lit.value); -}; - -template -struct handle_type_name> { - static constexpr auto name - = const_name("Literal[") + pybind11::detail::concat(lit.value) + const_name("]"); -}; - -// template -// struct handle_type_name> { -// static constexpr auto name -// = const_name("Literal[") + pybind11::detail::concat(intLit) + const_name("]"); -// }; - -// template -// struct handle_type_name> { -// static constexpr auto name -// = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); -// } - -// template <> -// struct handle_type_name> { -// static constexpr auto name -// = const_name("Literal[None]"); -// } - -// template -// struct handle_type_name> { -// // TODO handle conststr -// static constexpr auto name = const_name("Literal[") + pybind11::detail::concat(boolLit) + const_name("]"); -// } -#endif - -PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) template struct handle_type_name> { static constexpr auto name = const_name("type[") + make_caster::name + const_name("]"); @@ -271,9 +170,10 @@ struct handle_type_name> { }; #if defined(__cpp_nontype_template_parameter_class) -template -struct handle_type_name> { - static constexpr auto name = const_name(StrLit.value); +template +struct handle_type_name> { + static constexpr auto name + = const_name("Literal[") + pybind11::detail::concat(const_name(Literals.name)...) + const_name("]"); }; #endif diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index f4cae26360..69871c4e0d 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,12 +109,16 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject +namespace literals { #if defined(__cpp_nontype_template_parameter_class) -namespace typevar { -typedef py::typing::TypeVar<"T"> TypeVarT; -typedef py::typing::TypeVar<"V"> TypeVarV; -} // namespace typevar +enum Color { + RED = 0, + BLUE = 1 +}; + +typedef py::typing::Literal<"26", "0x1A", "\"hello world\"", "b\"hello world\"", "u\"hello world\"", "True", "Color.RED", "None"> LiteralFoo; #endif +} // namespace literals TEST_SUBMODULE(pytypes, m) { m.def("obj_class_name", [](py::handle obj) { return py::detail::obj_class_name(obj.ptr()); }); @@ -852,28 +856,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); -#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; }); - - typedef typing::Literal<"A", "B", "C"> stringLiteral; - m.def("annotate_str_literal", [](const stringLiteral s) -> py::str { return s; }); - typedef typing::Literal<1, 2, 3> intLiteral; - m.def("annotate_int_literal", [](const intLiteral i) -> py::int_ { return i; }); - typedef typing::Literal boolLiteral; - m.def("annotate_bool_literal", [](const boolLiteral i) -> py::bool_ { return i; }); - typedef typing::Literal noneLiteral; - m.def("annotate_none_literal", [](const noneLiteral i) -> py::none { return i; }); - typedef typing::Literal<"A", 1, true, py::none> anyLiteral; - m.def("annotate_any_literal", [](const anyLiteral i) -> py::object { return i; }); -#endif - m.def("annotate_type", [](const py::typing::Type &) {}); + m.def("annotate_type", [](const py::typing::Type &t) -> py::type {return t;}); m.def("annotate_union", [](py::typing::List> l, @@ -903,14 +886,12 @@ TEST_SUBMODULE(pytypes, m) { [](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; - }); + py::enum_(m, "Color") + .value("RED", literals::Color::RED) + .value("BLUE", literals::Color::BLUE); - 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_literal", + [](literals::LiteralFoo &o) -> py::object {return o;}); m.attr("if_defined__cpp_nontype_template_parameter_class") = true; #else m.attr("if_defined__cpp_nontype_template_parameter_class") = false; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index b117a722c2..2ff1e66629 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,71 +958,6 @@ def test_fn_annotations(doc): == "annotate_fn(arg0: Callable[[list[str], str], int]) -> None" ) - -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" - - -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" - - -def test_string_literal(doc): - with contextlib.suppress(AttributeError): - assert ( - doc(m.annotate_str_literal) - == 'annotate_str_literal(arg0: Literal["A", "B", C"]) -> str' - ) - - -def test_int_literal(doc): - with contextlib.suppress(AttributeError): - assert ( - doc(m.annotate_int_literal) - == "annotate_int_literal(arg0: Literal[1, 2, 3]) -> int" - ) - - -def test_bool_literal(doc): - with contextlib.suppress(AttributeError): - assert ( - doc(m.annotate_bool_literal) - == "annotate_bool_literal(arg0: Literal[True, False]) -> bool" - ) - - -def test_none_literal(doc): - with contextlib.suppress(AttributeError): - assert ( - doc(m.annotate_none_literal) - == "annotate_none_literal(arg0: Literal[None]) -> None" - ) - - -def test_any_literal(doc): - with contextlib.suppress(AttributeError): - assert ( - doc(m.annotate_any_literal) - == 'annotate_any_literal(arg0: Literal["A", 1, true, None]) -> object' - ) - - -# def test_literal_order(doc): -# with contextlib.suppress(AttributeError): -# assert ( -# doc(m.annotate_int_literal) -# == "annotate_object_to_T(arg0: Literal[1, 2, 3]) -> int" -# ) def test_type_annotation(doc): assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> type" @@ -1061,17 +996,12 @@ def test_optional_object_annotations(doc): == "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): +def test_literal(doc): assert ( - doc(m.annotate_generic_containers) - == "annotate_generic_containers(arg0: list[T]) -> list[V]" + doc(m.annotate_literal) + == "annotate_literal(arg0: Literal[26, 0x1A, \"hello world\", b\"hello world\", u\"hello world\", True, Color.RED, None]) -> object" ) - - 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 30c790e425c85f53288aba3cb683d3487f445e6d Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Mon, 24 Jun 2024 18:48:52 -0400 Subject: [PATCH 60/68] clean up --- include/pybind11/typing.h | 5 +++-- tests/test_pytypes.cpp | 26 +++++++++++++++----------- tests/test_pytypes.py | 4 +++- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 673adb3ef5..7da7fc658d 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -172,8 +172,9 @@ struct handle_type_name> { #if defined(__cpp_nontype_template_parameter_class) template struct handle_type_name> { - static constexpr auto name - = const_name("Literal[") + pybind11::detail::concat(const_name(Literals.name)...) + const_name("]"); + static constexpr auto name = const_name("Literal[") + + pybind11::detail::concat(const_name(Literals.name)...) + + const_name("]"); }; #endif diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 69871c4e0d..e64ff0aafb 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -111,12 +111,17 @@ void m_defs(py::module_ &m) { namespace literals { #if defined(__cpp_nontype_template_parameter_class) -enum Color { - RED = 0, - BLUE = 1 -}; - -typedef py::typing::Literal<"26", "0x1A", "\"hello world\"", "b\"hello world\"", "u\"hello world\"", "True", "Color.RED", "None"> LiteralFoo; +enum Color { RED = 0, BLUE = 1 }; + +typedef py::typing::Literal<"26", + "0x1A", + "\"hello world\"", + "b\"hello world\"", + "u\"hello world\"", + "True", + "Color.RED", + "None"> + LiteralFoo; #endif } // namespace literals @@ -856,7 +861,7 @@ TEST_SUBMODULE(pytypes, m) { 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, @@ -887,11 +892,10 @@ TEST_SUBMODULE(pytypes, m) { #if defined(__cpp_nontype_template_parameter_class) py::enum_(m, "Color") - .value("RED", literals::Color::RED) - .value("BLUE", literals::Color::BLUE); + .value("RED", literals::Color::RED) + .value("BLUE", literals::Color::BLUE); - m.def("annotate_literal", - [](literals::LiteralFoo &o) -> py::object {return o;}); + m.def("annotate_literal", [](literals::LiteralFoo &o) -> py::object { return o; }); m.attr("if_defined__cpp_nontype_template_parameter_class") = true; #else m.attr("if_defined__cpp_nontype_template_parameter_class") = false; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 2ff1e66629..f0c1b99ee3 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -958,6 +958,7 @@ def test_fn_annotations(doc): == "annotate_fn(arg0: Callable[[list[str], str], int]) -> None" ) + def test_type_annotation(doc): assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> type" @@ -996,6 +997,7 @@ def test_optional_object_annotations(doc): == "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.", @@ -1003,5 +1005,5 @@ def test_optional_object_annotations(doc): def test_literal(doc): assert ( doc(m.annotate_literal) - == "annotate_literal(arg0: Literal[26, 0x1A, \"hello world\", b\"hello world\", u\"hello world\", True, Color.RED, None]) -> object" + == 'annotate_literal(arg0: Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]) -> object' ) From b65b945f18def8017311cb0e31b48f30c362bf86 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Mon, 24 Jun 2024 18:50:29 -0400 Subject: [PATCH 61/68] Update comment --- 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 7da7fc658d..a6cc64a16b 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -80,13 +80,14 @@ class Optional : public object { using object::object; }; -// Also due to how C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] #if defined(__cpp_nontype_template_parameter_class) template struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } char name[N]; }; + +// NOTE: C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] template class Literal : public object { PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) From 00ba9fa075c72d97e03eb716dc93b501193b0a04 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 08:38:12 -0400 Subject: [PATCH 62/68] remove incorrect comment --- include/pybind11/typing.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index a6cc64a16b..0001339646 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -87,7 +87,6 @@ struct StringLiteral { char name[N]; }; -// NOTE: C++ implemented constant template Literal[1, 2] does not equal Literal[2, 1] template class Literal : public object { PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) From 2cb5b03088bd7171334808e3aab84b5fe7bf04c8 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 08:41:19 -0400 Subject: [PATCH 63/68] rerun CI --- 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 0001339646..7f8e568687 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -86,7 +86,7 @@ struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } char name[N]; }; - + template class Literal : public object { PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) From 3d20c57e7c97a0326b504a53b574f3f28d709028 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 08:41:28 -0400 Subject: [PATCH 64/68] rerun CI --- 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 7f8e568687..0001339646 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -86,7 +86,7 @@ struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, name); } char name[N]; }; - + template class Literal : public object { PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) From 02232447ca841c1db614d81c3ebf5bb15a2618b6 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 17:17:43 -0400 Subject: [PATCH 65/68] fix extra line --- tests/test_pytypes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index c6b42addf7..1e0001027a 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1011,7 +1011,6 @@ def test_literal(doc): not m.if_defined__cpp_nontype_template_parameter_class, reason="C++20 feature not available.", ) -def te def test_typevar(doc): assert ( doc(m.annotate_generic_containers) From 0f302d9756eb314beae5e615783f87eb9713fa6f Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 17:18:36 -0400 Subject: [PATCH 66/68] lint --- tests/test_pytypes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 1e0001027a..b265512c8e 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1007,6 +1007,8 @@ def test_literal(doc): doc(m.annotate_literal) == 'annotate_literal(arg0: Literal[26, 0x1A, "hello world", b"hello world", u"hello world", True, Color.RED, None]) -> object' ) + + @pytest.mark.skipif( not m.if_defined__cpp_nontype_template_parameter_class, reason="C++20 feature not available.", From befcb7885737df7d3cd29215fb0aa0241256a976 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 17:22:48 -0400 Subject: [PATCH 67/68] move if defined block --- tests/test_pytypes.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 97db8a10eb..84400f22a7 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,8 +109,9 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject -namespace literals { + #if defined(__cpp_nontype_template_parameter_class) +namespace literals { enum Color { RED = 0, BLUE = 1 }; typedef py::typing::Literal<"26", From dcf8e490f3c6917aa553a8b2d291f80d0a8d1664 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Jun 2024 21:23:09 +0000 Subject: [PATCH 68/68] style: pre-commit fixes --- tests/test_pytypes.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 84400f22a7..952b5af6eb 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -109,7 +109,6 @@ void m_defs(py::module_ &m) { } // namespace handle_from_move_only_type_with_operator_PyObject - #if defined(__cpp_nontype_template_parameter_class) namespace literals { enum Color { RED = 0, BLUE = 1 };