Skip to content

Commit c29e9cb

Browse files
committed
use py::is_operator(), to keep the NotImplemented exception behavior, but with a much smaller change
1 parent 4d699d5 commit c29e9cb

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

src/duckdb_py/typing/pytype.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,9 @@ static LogicalType FromObject(const py::object &object) {
325325
void DuckDBPyType::Initialize(py::handle &m) {
326326
auto type_module = py::class_<DuckDBPyType, shared_ptr<DuckDBPyType>>(m, "DuckDBPyType", py::module_local());
327327

328-
type_module.def("__repr__", &DuckDBPyType::ToString, "Stringified representation of the type object")
329-
.def("__eq__", [](const DuckDBPyType &self, py::handle other) -> py::object {
330-
if (py::isinstance<DuckDBPyType>(other)) {
331-
return py::bool_(self.Equals(other.cast<shared_ptr<DuckDBPyType>>()));
332-
}
333-
else if (py::isinstance<py::str>(other)) {
334-
return py::bool_(self.EqualsString(other.cast<string>()));
335-
}
336-
// Return NotImplemented for unsupported types
337-
return py::reinterpret_borrow<py::object>(Py_NotImplemented);
338-
});
339-
type_module.def("__eq__", &DuckDBPyType::EqualsString, "Compare two types for equality", py::arg("other"));
328+
type_module.def("__repr__", &DuckDBPyType::ToString, "Stringified representation of the type object");
329+
type_module.def("__eq__", &DuckDBPyType::Equals, "Compare two types for equality", py::arg("other"), py::is_operator());
330+
type_module.def("__eq__", &DuckDBPyType::EqualsString, "Compare two types for equality", py::arg("other"), py::is_operator());
340331
type_module.def_property_readonly("id", &DuckDBPyType::GetId);
341332
type_module.def_property_readonly("children", &DuckDBPyType::Children);
342333
type_module.def(py::init<>([](const string &type_str, shared_ptr<DuckDBPyConnection> connection = nullptr) {
@@ -356,7 +347,7 @@ void DuckDBPyType::Initialize(py::handle &m) {
356347
return make_shared_ptr<DuckDBPyType>(ltype);
357348
}));
358349
type_module.def("__getattr__", &DuckDBPyType::GetAttribute, "Get the child type by 'name'", py::arg("name"));
359-
type_module.def("__getitem__", &DuckDBPyType::GetAttribute, "Get the child type by 'name'", py::arg("name"));
350+
type_module.def("__getitem__", &DuckDBPyType::GetAttribute, "Get the child type by 'name'", py::arg("name"), py::is_operator());
360351

361352
py::implicitly_convertible<py::object, DuckDBPyType>();
362353
py::implicitly_convertible<py::str, DuckDBPyType>();

0 commit comments

Comments
 (0)