diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 0e6eb8485..20919c4ab 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 c1cffd0f6..aa77a65b0 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 {};