From bb829344b5cd95e95550c3af2ee9af4055c5c2bd Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:03:06 -0400 Subject: [PATCH 01/16] prototype --- include/pybind11/pytypes.h | 9 +++++++++ include/pybind11/typing.h | 37 ++++++++++++++++++++++++++++++++++++- tests/test_pytypes.cpp | 17 +++++++++++++++++ tests/test_pytypes.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index f26c307a87..c846f48a61 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -182,6 +182,10 @@ class object_api : public pyobject_tag { /// Get or set the object's docstring, i.e. ``obj.__doc__``. str_attr_accessor doc() const; + /// Get or set the object's type_params, i.e. ``obj.__type_params__``. + str_attr_accessor type_params() const; + + /// Return the object's current reference count ssize_t ref_count() const { #ifdef PYPY_VERSION @@ -2534,6 +2538,11 @@ str_attr_accessor object_api::doc() const { return attr("__doc__"); } +template +str_attr_accessor object_api::type_params() const { + return attr("__type_params__"); +} + template handle object_api::get_type() const { return type::handle_of(derived()); diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index b0feb9464a..077eaaf465 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -115,7 +115,7 @@ class Literal : public object { PYBIND11_OBJECT_DEFAULT(Literal, object, PyObject_Type) }; -// Example syntax for creating a TypeVar. +// Example syntax for creating a type annotation of a TypeVar, ParamSpec, and TypeVarTuple. // typedef typing::TypeVar<"T"> TypeVarT; template class TypeVar : public object { @@ -124,6 +124,41 @@ class TypeVar : public object { }; #endif + + +template +class TypeVarObject : public object { + PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) + using object::object; + TypeVarObject(const char *name){ + attr("__name__") = name; + attr("__bound__") = make_caster; + attr("__constraints__") = pybind11::make_tuple(); + } + // TypeVarObject(const char *name, py::typing::Tuple tuple){ + // attr("__name__") = name; + // attr("__bound__") = py::none(); + // attr("__constraints__") = tuple; + // } +}; + +class ParamSpec : public object { + PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type) + using object::object; + ParamSpec(const char *name){ + attr("__name__") = name; + attr("__bound__") = pybind11::none(); + } +}; + +class TypeVarTuple : public object { + PYBIND11_OBJECT_DEFAULT(TypeVarTuple, object, PyObject_Type) + using object::object; + TypeVarTuple(const char *name){ + attr("__name__") = name; + } +}; + PYBIND11_NAMESPACE_END(typing) PYBIND11_NAMESPACE_BEGIN(detail) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index ecb44939aa..aa77a65b0c 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -923,4 +923,21 @@ TEST_SUBMODULE(pytypes, m) { #else m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false; #endif + + struct TypeVarObject {}; + py::class_(m, "TypeVarObject").type_params() = py::make_tuple(py::typing::TypeVarObject("T")); + + struct ParamSpec {}; + py::class_(m, "ParamSpec").type_params() = py::make_tuple(py::typing::ParamSpec("P")); + + struct TypeVarTuple {}; + py::class_(m, "TypeVarTuple").type_params() = py::make_tuple(py::typing::TypeVarTuple("T")); + + + struct NoTypeParams {}; + struct TypeParams {}; + py::class_(m, "NoTypeParams"); + // TODO: Use custom objects + py::class_(m, "TypeParams").type_params() = py::make_tuple("foo", 3, py::none()); + m.def("type_params", []() -> void {}).type_params() = py::make_tuple("foo", 3, py::none()); } diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 218092b434..ff1825784b 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -4,6 +4,7 @@ import sys import types +from typing import TypeVar import pytest import env @@ -1048,3 +1049,30 @@ 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_typevar_object(): + assert len(m.TypeVarObject.__type_params__) == 1 + type_var = m.TypeVarObject.__type_params__[0] + assert type_var.__name__ == "T" + assert type_var.__bound__ == int + assert type_var.__constraints__ == () + +def test_param_spec(): + assert len(m.ParamSpec.__type_params__) == 1 + param_spec = m.ParamSpec.__type_params__[0] + + assert param_spec.__name__ == "P" + assert param_spec.__bound__ == None + +def test_type_var_tuple(): + assert len(m.TypeVarTuple.__type_params__) == 1 + type_var_tuple = m.TypeVarTuple.__type_params__[0] + + assert type_var_tuple.__name__ == "T" + with pytest.raises(AttributeError): + param_spec.__bound__ + +def test_type_params(): + assert m.NoTypeParams.__type_params__ == () + assert m.TypeParams.__type_params__ == ("foo", 3, None) + assert m.type_params.__type_params__ == ("foo", 3, None) From 445fee025cdbc44dcff1b0800452f52ecebf0939 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:11:21 -0400 Subject: [PATCH 02/16] remove unused import --- tests/test_pytypes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index ff1825784b..6159273fe0 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -4,7 +4,6 @@ import sys import types -from typing import TypeVar import pytest import env From 1ad0e6ebfabac42b6f123d2494cbafd39c45ee3e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:14:15 +0000 Subject: [PATCH 03/16] style: pre-commit fixes --- include/pybind11/pytypes.h | 1 - include/pybind11/typing.h | 10 +++------- tests/test_pytypes.cpp | 12 +++++++----- tests/test_pytypes.py | 4 ++++ 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index c846f48a61..c70773ad06 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -185,7 +185,6 @@ class object_api : public pyobject_tag { /// Get or set the object's type_params, i.e. ``obj.__type_params__``. str_attr_accessor type_params() const; - /// Return the object's current reference count ssize_t ref_count() const { #ifdef PYPY_VERSION diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 077eaaf465..0e6eb84850 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -124,13 +124,11 @@ class TypeVar : public object { }; #endif - - template class TypeVarObject : public object { PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) using object::object; - TypeVarObject(const char *name){ + TypeVarObject(const char *name) { attr("__name__") = name; attr("__bound__") = make_caster; attr("__constraints__") = pybind11::make_tuple(); @@ -145,7 +143,7 @@ class TypeVarObject : public object { class ParamSpec : public object { PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type) using object::object; - ParamSpec(const char *name){ + ParamSpec(const char *name) { attr("__name__") = name; attr("__bound__") = pybind11::none(); } @@ -154,9 +152,7 @@ class ParamSpec : public object { class TypeVarTuple : public object { PYBIND11_OBJECT_DEFAULT(TypeVarTuple, object, PyObject_Type) using object::object; - TypeVarTuple(const char *name){ - attr("__name__") = name; - } + TypeVarTuple(const char *name) { attr("__name__") = name; } }; PYBIND11_NAMESPACE_END(typing) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index aa77a65b0c..c1cffd0f61 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -925,14 +925,16 @@ TEST_SUBMODULE(pytypes, m) { #endif struct TypeVarObject {}; - py::class_(m, "TypeVarObject").type_params() = py::make_tuple(py::typing::TypeVarObject("T")); - + py::class_(m, "TypeVarObject").type_params() + = py::make_tuple(py::typing::TypeVarObject("T")); + struct ParamSpec {}; - py::class_(m, "ParamSpec").type_params() = py::make_tuple(py::typing::ParamSpec("P")); + py::class_(m, "ParamSpec").type_params() + = py::make_tuple(py::typing::ParamSpec("P")); struct TypeVarTuple {}; - py::class_(m, "TypeVarTuple").type_params() = py::make_tuple(py::typing::TypeVarTuple("T")); - + py::class_(m, "TypeVarTuple").type_params() + = py::make_tuple(py::typing::TypeVarTuple("T")); struct NoTypeParams {}; struct TypeParams {}; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 6159273fe0..0b31cb0d31 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1049,6 +1049,7 @@ def test_typevar(doc): assert doc(m.annotate_object_to_T) == "annotate_object_to_T(arg0: object) -> T" + def test_typevar_object(): assert len(m.TypeVarObject.__type_params__) == 1 type_var = m.TypeVarObject.__type_params__[0] @@ -1056,6 +1057,7 @@ def test_typevar_object(): assert type_var.__bound__ == int assert type_var.__constraints__ == () + def test_param_spec(): assert len(m.ParamSpec.__type_params__) == 1 param_spec = m.ParamSpec.__type_params__[0] @@ -1063,6 +1065,7 @@ def test_param_spec(): assert param_spec.__name__ == "P" assert param_spec.__bound__ == None + def test_type_var_tuple(): assert len(m.TypeVarTuple.__type_params__) == 1 type_var_tuple = m.TypeVarTuple.__type_params__[0] @@ -1071,6 +1074,7 @@ def test_type_var_tuple(): with pytest.raises(AttributeError): param_spec.__bound__ + def test_type_params(): assert m.NoTypeParams.__type_params__ == () assert m.TypeParams.__type_params__ == ("foo", 3, None) From 07ccee567f9ef0f41593efc8ce58b830d5447c20 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:16:49 -0400 Subject: [PATCH 04/16] fix unused variable --- tests/test_pytypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 0b31cb0d31..bb6291f493 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1063,7 +1063,7 @@ def test_param_spec(): param_spec = m.ParamSpec.__type_params__[0] assert param_spec.__name__ == "P" - assert param_spec.__bound__ == None + assert param_spec.__bound__ is None def test_type_var_tuple(): @@ -1072,7 +1072,7 @@ def test_type_var_tuple(): assert type_var_tuple.__name__ == "T" with pytest.raises(AttributeError): - param_spec.__bound__ + print(type_var_tuple.__bound__) def test_type_params(): From c6ba82cbba0fb9df3a0eb539e8b6dacdfe62d491 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:21:41 -0400 Subject: [PATCH 05/16] try name wrapper? --- include/pybind11/typing.h | 10 +++++++++- tests/test_pytypes.cpp | 12 +++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 0e6eb84850..20919c4ab6 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -124,13 +124,21 @@ class TypeVar : public object { }; #endif +class NameWrapper : public object { + PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) + using object::object; + NameWrapper(const char *name){ + attr("__name__") = name; + } +} + template class TypeVarObject : public object { PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) using object::object; TypeVarObject(const char *name) { attr("__name__") = name; - attr("__bound__") = make_caster; + attr("__bound__") = NameWrapper(make_caster::name); attr("__constraints__") = pybind11::make_tuple(); } // TypeVarObject(const char *name, py::typing::Tuple tuple){ diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index c1cffd0f61..aa77a65b0c 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -925,16 +925,14 @@ TEST_SUBMODULE(pytypes, m) { #endif struct TypeVarObject {}; - py::class_(m, "TypeVarObject").type_params() - = py::make_tuple(py::typing::TypeVarObject("T")); - + py::class_(m, "TypeVarObject").type_params() = py::make_tuple(py::typing::TypeVarObject("T")); + struct ParamSpec {}; - py::class_(m, "ParamSpec").type_params() - = py::make_tuple(py::typing::ParamSpec("P")); + py::class_(m, "ParamSpec").type_params() = py::make_tuple(py::typing::ParamSpec("P")); struct TypeVarTuple {}; - py::class_(m, "TypeVarTuple").type_params() - = py::make_tuple(py::typing::TypeVarTuple("T")); + py::class_(m, "TypeVarTuple").type_params() = py::make_tuple(py::typing::TypeVarTuple("T")); + struct NoTypeParams {}; struct TypeParams {}; From 960d4bcd95856546685734f84b489c3ca6abb8ee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:22:40 +0000 Subject: [PATCH 06/16] style: pre-commit fixes --- include/pybind11/typing.h | 4 +--- tests/test_pytypes.cpp | 12 +++++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 20919c4ab6..2763239f45 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -127,9 +127,7 @@ class TypeVar : public object { class NameWrapper : public object { PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) using object::object; - NameWrapper(const char *name){ - attr("__name__") = name; - } + NameWrapper(const char *name) { attr("__name__") = name; } } template diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index aa77a65b0c..c1cffd0f61 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -925,14 +925,16 @@ TEST_SUBMODULE(pytypes, m) { #endif struct TypeVarObject {}; - py::class_(m, "TypeVarObject").type_params() = py::make_tuple(py::typing::TypeVarObject("T")); - + py::class_(m, "TypeVarObject").type_params() + = py::make_tuple(py::typing::TypeVarObject("T")); + struct ParamSpec {}; - py::class_(m, "ParamSpec").type_params() = py::make_tuple(py::typing::ParamSpec("P")); + py::class_(m, "ParamSpec").type_params() + = py::make_tuple(py::typing::ParamSpec("P")); struct TypeVarTuple {}; - py::class_(m, "TypeVarTuple").type_params() = py::make_tuple(py::typing::TypeVarTuple("T")); - + py::class_(m, "TypeVarTuple").type_params() + = py::make_tuple(py::typing::TypeVarTuple("T")); struct NoTypeParams {}; struct TypeParams {}; From 6f07805421a186ed333e780033c0fc6dd755c308 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:26:50 -0400 Subject: [PATCH 07/16] use detail:: --- 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 2763239f45..5375659b4b 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -136,7 +136,7 @@ class TypeVarObject : public object { using object::object; TypeVarObject(const char *name) { attr("__name__") = name; - attr("__bound__") = NameWrapper(make_caster::name); + attr("__bound__") = NameWrapper(pybind11::detail::make_caster::name); attr("__constraints__") = pybind11::make_tuple(); } // TypeVarObject(const char *name, py::typing::Tuple tuple){ From 5fb5ed9ab51dd75441d0b2b6b4b16e77051a1b42 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:29:21 -0400 Subject: [PATCH 08/16] fix PYBIND11_OBJECT_DEFUALT --- 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 5375659b4b..f6e0a2916a 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -125,10 +125,10 @@ class TypeVar : public object { #endif class NameWrapper : public object { - PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) + PYBIND11_OBJECT_DEFAULT(NameWrapper, object, PyObject_Type) using object::object; NameWrapper(const char *name) { attr("__name__") = name; } -} +}; template class TypeVarObject : public object { From 2567f44fa20f5be4757fea604e2b8bb8a428512a Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:38:01 -0400 Subject: [PATCH 09/16] try no name wrapper --- include/pybind11/typing.h | 24 ++++++++++++++++++------ tests/test_pytypes.cpp | 4 ++++ tests/test_pytypes.py | 13 +++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index f6e0a2916a..84baeb3f37 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -124,11 +124,11 @@ class TypeVar : public object { }; #endif -class NameWrapper : public object { - PYBIND11_OBJECT_DEFAULT(NameWrapper, object, PyObject_Type) - using object::object; - NameWrapper(const char *name) { attr("__name__") = name; } -}; +// class NameWrapper : public object { +// PYBIND11_OBJECT_DEFAULT(NameWrapper, object, PyObject_Type) +// using object::object; +// NameWrapper(const char *name) { attr("__name__") = name; } +// }; template class TypeVarObject : public object { @@ -136,7 +136,8 @@ class TypeVarObject : public object { using object::object; TypeVarObject(const char *name) { attr("__name__") = name; - attr("__bound__") = NameWrapper(pybind11::detail::make_caster::name); + attr("__bound__") = object() + attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; attr("__constraints__") = pybind11::make_tuple(); } // TypeVarObject(const char *name, py::typing::Tuple tuple){ @@ -146,6 +147,17 @@ class TypeVarObject : public object { // } }; +template <> +class TypeVarObject : public object { + PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) + using object::object; + TypeVarObject(const char *name) { + attr("__name__") = name; + attr("__bound__") = py::none(); + attr("__constraints__") = pybind11::make_tuple(); + } +}; + class ParamSpec : public object { PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type) using object::object; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index c1cffd0f61..3ca418bfdb 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -926,6 +926,10 @@ TEST_SUBMODULE(pytypes, m) { struct TypeVarObject {}; py::class_(m, "TypeVarObject").type_params() + = py::make_tuple(py::typing::TypeVarObject<>("T")); + + struct TypeVarObjectBound {}; + py::class_(m, "TypeVarObjectBound").type_params() = py::make_tuple(py::typing::TypeVarObject("T")); struct ParamSpec {}; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index bb6291f493..0fb78451cb 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1054,9 +1054,22 @@ def test_typevar_object(): assert len(m.TypeVarObject.__type_params__) == 1 type_var = m.TypeVarObject.__type_params__[0] assert type_var.__name__ == "T" + assert type_var.__bound__ == None + assert type_var.__constraints__ == () + + + assert len(m.TypeVarObjectBound.__type_params__) == 1 + type_var = m.TypeVarObjectBound.__type_params__[0] + assert type_var.__name__ == "T" assert type_var.__bound__ == int assert type_var.__constraints__ == () + assert len(m.TypeVarObjectConstraints.__type_params__) == 1 + type_var = m.TypeVarObjectConstraints.__type_params__[0] + assert type_var.__name__ == "T" + assert type_var.__bound__ == None + assert type_var.__constraints__ == ("hi", 3) + def test_param_spec(): assert len(m.ParamSpec.__type_params__) == 1 From 8214dad571845bfe967960d75ed9ecbd518b4035 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:40:01 +0000 Subject: [PATCH 10/16] style: pre-commit fixes --- include/pybind11/typing.h | 4 ++-- tests/test_pytypes.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 84baeb3f37..83d73bc92d 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -136,8 +136,8 @@ class TypeVarObject : public object { using object::object; TypeVarObject(const char *name) { attr("__name__") = name; - attr("__bound__") = object() - attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; + attr("__bound__") = object() attr("__bound__").attr("__name__") + = pybind11::detail::make_caster::name; attr("__constraints__") = pybind11::make_tuple(); } // TypeVarObject(const char *name, py::typing::Tuple tuple){ diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 0fb78451cb..59b574ba35 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1057,7 +1057,6 @@ def test_typevar_object(): assert type_var.__bound__ == None assert type_var.__constraints__ == () - assert len(m.TypeVarObjectBound.__type_params__) == 1 type_var = m.TypeVarObjectBound.__type_params__[0] assert type_var.__name__ == "T" From 9e7ae0d99c3f41caa24bfa0f943afa195c6048c0 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:40:35 -0400 Subject: [PATCH 11/16] add missing semi colon --- 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 83d73bc92d..b2dab0e351 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -136,7 +136,8 @@ class TypeVarObject : public object { using object::object; TypeVarObject(const char *name) { attr("__name__") = name; - attr("__bound__") = object() attr("__bound__").attr("__name__") + attr("__bound__") = object(); + attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; attr("__constraints__") = pybind11::make_tuple(); } From e98fb5a10ee08fd5585836319ff4865f50f67891 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:41:46 +0000 Subject: [PATCH 12/16] style: pre-commit fixes --- include/pybind11/typing.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index b2dab0e351..3f28543228 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -136,9 +136,8 @@ class TypeVarObject : public object { using object::object; TypeVarObject(const char *name) { attr("__name__") = name; - attr("__bound__") = object(); - attr("__bound__").attr("__name__") - = pybind11::detail::make_caster::name; + attr("__bound__") = object(); + attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; attr("__constraints__") = pybind11::make_tuple(); } // TypeVarObject(const char *name, py::typing::Tuple tuple){ From 479dffd98e187ba9f2612d9df0f7ef19419c147e Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:43:27 -0400 Subject: [PATCH 13/16] is None --- tests/test_pytypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 59b574ba35..cd19bb4bec 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1054,7 +1054,7 @@ def test_typevar_object(): assert len(m.TypeVarObject.__type_params__) == 1 type_var = m.TypeVarObject.__type_params__[0] assert type_var.__name__ == "T" - assert type_var.__bound__ == None + assert type_var.__bound__ is None assert type_var.__constraints__ == () assert len(m.TypeVarObjectBound.__type_params__) == 1 @@ -1066,7 +1066,7 @@ def test_typevar_object(): assert len(m.TypeVarObjectConstraints.__type_params__) == 1 type_var = m.TypeVarObjectConstraints.__type_params__[0] assert type_var.__name__ == "T" - assert type_var.__bound__ == None + assert type_var.__bound__ is None assert type_var.__constraints__ == ("hi", 3) From d1dc670142261f11c2581a6da6ed8e5adc837806 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:47:08 -0400 Subject: [PATCH 14/16] skip typevar for now --- tests/test_pytypes.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 3ca418bfdb..9c480be1ef 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -924,13 +924,13 @@ TEST_SUBMODULE(pytypes, m) { m.attr("defined_PYBIND11_TYPING_H_HAS_STRING_LITERAL") = false; #endif - struct TypeVarObject {}; - py::class_(m, "TypeVarObject").type_params() - = py::make_tuple(py::typing::TypeVarObject<>("T")); + // struct TypeVarObject {}; + // py::class_(m, "TypeVarObject").type_params() + // = py::make_tuple(py::typing::TypeVarObject<>("T")); - struct TypeVarObjectBound {}; - py::class_(m, "TypeVarObjectBound").type_params() - = py::make_tuple(py::typing::TypeVarObject("T")); + // struct TypeVarObjectBound {}; + // py::class_(m, "TypeVarObjectBound").type_params() + // = py::make_tuple(py::typing::TypeVarObject("T")); struct ParamSpec {}; py::class_(m, "ParamSpec").type_params() From fd7d4d0c9b2e4cb8163c948258eb27aab495a6c7 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Fri, 26 Jul 2024 10:47:30 -0400 Subject: [PATCH 15/16] skip typevar --- include/pybind11/typing.h | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 3f28543228..8d5fe07e39 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -130,33 +130,33 @@ class TypeVar : public object { // NameWrapper(const char *name) { attr("__name__") = name; } // }; -template -class TypeVarObject : public object { - PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) - using object::object; - TypeVarObject(const char *name) { - attr("__name__") = name; - attr("__bound__") = object(); - attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; - attr("__constraints__") = pybind11::make_tuple(); - } - // TypeVarObject(const char *name, py::typing::Tuple tuple){ - // attr("__name__") = name; - // attr("__bound__") = py::none(); - // attr("__constraints__") = tuple; - // } -}; +// template +// class TypeVarObject : public object { +// PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) +// using object::object; +// TypeVarObject(const char *name) { +// attr("__name__") = name; +// attr("__bound__") = object(); +// attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; +// attr("__constraints__") = pybind11::make_tuple(); +// } +// // TypeVarObject(const char *name, py::typing::Tuple tuple){ +// // attr("__name__") = name; +// // attr("__bound__") = py::none(); +// // attr("__constraints__") = tuple; +// // } +// }; -template <> -class TypeVarObject : public object { - PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) - using object::object; - TypeVarObject(const char *name) { - attr("__name__") = name; - attr("__bound__") = py::none(); - attr("__constraints__") = pybind11::make_tuple(); - } -}; +// template <> +// class TypeVarObject : public object { +// PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type) +// using object::object; +// TypeVarObject(const char *name) { +// attr("__name__") = name; +// attr("__bound__") = py::none(); +// attr("__constraints__") = pybind11::make_tuple(); +// } +// }; class ParamSpec : public object { PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type) From 948c1d3113fe5ee17374434211925e3e0194e367 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:47:55 +0000 Subject: [PATCH 16/16] 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 8d5fe07e39..683535e755 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -140,7 +140,8 @@ class TypeVar : public object { // attr("__bound__").attr("__name__") = pybind11::detail::make_caster::name; // attr("__constraints__") = pybind11::make_tuple(); // } -// // TypeVarObject(const char *name, py::typing::Tuple tuple){ +// // TypeVarObject(const char *name, py::typing::Tuple +// tuple){ // // attr("__name__") = name; // // attr("__bound__") = py::none(); // // attr("__constraints__") = tuple;