From 58e5a0967034ce5806f05772597a9f3a44768e28 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 17 Dec 2024 13:01:50 -0800 Subject: [PATCH] Add guard: __annotations__["list_int"] was set already. --- include/pybind11/cast.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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}; }