diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index f6e0a2916..84baeb3f3 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 c1cffd0f6..3ca418bfd 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 bb6291f49..0fb78451c 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