try no name wrapper

This commit is contained in:
Michael Carlstrom 2024-07-26 10:38:01 -04:00
parent 5fb5ed9ab5
commit 2567f44fa2
3 changed files with 35 additions and 6 deletions

View File

@ -124,11 +124,11 @@ class TypeVar : public object {
}; };
#endif #endif
class NameWrapper : public object { // class NameWrapper : public object {
PYBIND11_OBJECT_DEFAULT(NameWrapper, object, PyObject_Type) // PYBIND11_OBJECT_DEFAULT(NameWrapper, object, PyObject_Type)
using object::object; // using object::object;
NameWrapper(const char *name) { attr("__name__") = name; } // NameWrapper(const char *name) { attr("__name__") = name; }
}; // };
template <typename T> template <typename T>
class TypeVarObject : public object { class TypeVarObject : public object {
@ -136,7 +136,8 @@ class TypeVarObject : public object {
using object::object; using object::object;
TypeVarObject(const char *name) { TypeVarObject(const char *name) {
attr("__name__") = name; attr("__name__") = name;
attr("__bound__") = NameWrapper(pybind11::detail::make_caster<T>::name); attr("__bound__") = object()
attr("__bound__").attr("__name__") = pybind11::detail::make_caster<T>::name;
attr("__constraints__") = pybind11::make_tuple(); attr("__constraints__") = pybind11::make_tuple();
} }
// TypeVarObject(const char *name, py::typing::Tuple<pybind11::type, pybind11::ellipse> tuple){ // TypeVarObject(const char *name, py::typing::Tuple<pybind11::type, pybind11::ellipse> 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 { class ParamSpec : public object {
PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type) PYBIND11_OBJECT_DEFAULT(ParamSpec, object, PyObject_Type)
using object::object; using object::object;

View File

@ -926,6 +926,10 @@ TEST_SUBMODULE(pytypes, m) {
struct TypeVarObject {}; struct TypeVarObject {};
py::class_<TypeVarObject>(m, "TypeVarObject").type_params() py::class_<TypeVarObject>(m, "TypeVarObject").type_params()
= py::make_tuple(py::typing::TypeVarObject<>("T"));
struct TypeVarObjectBound {};
py::class_<TypeVarObjectBound>(m, "TypeVarObjectBound").type_params()
= py::make_tuple(py::typing::TypeVarObject<int>("T")); = py::make_tuple(py::typing::TypeVarObject<int>("T"));
struct ParamSpec {}; struct ParamSpec {};

View File

@ -1054,9 +1054,22 @@ def test_typevar_object():
assert len(m.TypeVarObject.__type_params__) == 1 assert len(m.TypeVarObject.__type_params__) == 1
type_var = m.TypeVarObject.__type_params__[0] type_var = m.TypeVarObject.__type_params__[0]
assert type_var.__name__ == "T" 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.__bound__ == int
assert type_var.__constraints__ == () 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(): def test_param_spec():
assert len(m.ParamSpec.__type_params__) == 1 assert len(m.ParamSpec.__type_params__) == 1