diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 4a10dcbbf..ffd3d4dd3 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1376,7 +1376,11 @@ str_attr_accessor object_api::attr_with_type_hint(const char *key) const { "C++17 feature __cpp_inline_variables not available: " "https://en.cppreference.com/w/cpp/language/static#Static_data_members"); #endif - annotations()[key] = make_caster::name.text; + object ann = annotations(); + if (ann.contains(key)) { + throw std::runtime_error("__annotations__[\"" + std::string(key) + "\"] was set already."); + } + ann[key] = make_caster::name.text; return {derived(), key}; }