Skip to content

Commit

Permalink
try no name wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
InvincibleRMC committed Jul 26, 2024
1 parent 5fb5ed9 commit 2567f44
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
24 changes: 18 additions & 6 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,20 @@ 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 <typename T>
class TypeVarObject : public object {
PYBIND11_OBJECT_DEFAULT(TypeVarObject, object, PyObject_Type)
using object::object;
TypeVarObject(const char *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();
}
// TypeVarObject(const char *name, py::typing::Tuple<pybind11::type, pybind11::ellipse> tuple){
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,10 @@ TEST_SUBMODULE(pytypes, m) {

struct TypeVarObject {};
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"));

struct ParamSpec {};
Expand Down
13 changes: 13 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2567f44

Please sign in to comment.