From ba5f6301c40b91f86634cac89932301c73e25b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Mon, 26 Dec 2022 17:57:36 +0100 Subject: [PATCH] py: Fix __contains__ not converting to map key type since pybind11 2.8.0 Fixes a pybind11 API breaking change that was uncovered by issue #25 --- py/pybind11_common.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/py/pybind11_common.h b/py/pybind11_common.h index 13dd598..1d22a6a 100644 --- a/py/pybind11_common.h +++ b/py/pybind11_common.h @@ -140,6 +140,17 @@ py::class_ BindMap(py::handle scope, const std::string& name, }), "dictionary"_a) .def(py::self == py::self) + .def( + "__contains__", + [](const Map& map, const py::object& arg) { + try { + auto key = py::cast(arg); + return map.find(key) != map.end(); + } catch (const py::cast_error&) { + return false; + } + }, + py::prepend{}) .def("clear", &Map::clear) .def( "get",