diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 82ec2b6d0..df18157db 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1366,11 +1366,8 @@ object object_or_cast(T &&o) { return pybind11::cast(std::forward(o)); } -// This is being used to get around the conflict with the deprecated str() function on object_api -using py_str = str; - // Declared in pytypes.h: -// Written here so make_caster can be used +// Implemented here so that make_caster can be used. template template str_attr_accessor object_api::attr_with_type_hint(const char *key) const { @@ -1390,19 +1387,8 @@ str_attr_accessor object_api::attr_with_type_hint(const char *key) const { template template obj_attr_accessor object_api::attr_with_type_hint(handle key) const { -#if !defined(__cpp_inline_variables) - static_assert(!std::is_same::value, - "C++17 feature __cpp_inline_variables not available: " - "https://en.cppreference.com/w/cpp/language/static#Static_data_members"); -#endif - object ann = annotations(); - auto reinterpreted_key = reinterpret_borrow(key); - if (ann.contains(reinterpreted_key)) { - throw std::runtime_error("__annotations__[\"" + std::string(py_str(reinterpreted_key)) - + "\"] was set already."); - } - ann[key] = make_caster::name.text; - return {derived(), reinterpreted_key}; + (void) attr_with_type_hint(key.cast().c_str()); + return {derived(), reinterpret_borrow(key)}; } // Placeholder type for the unneeded (and dead code) static variable in the diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index d8d158b95..def7a122f 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -116,7 +116,6 @@ public: // attr_with_type_hint is implemented in cast.h: template obj_attr_accessor attr_with_type_hint(handle key) const; - template str_attr_accessor attr_with_type_hint(const char *key) const; diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 311aa50b5..b4fa99192 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -1039,8 +1039,10 @@ TEST_SUBMODULE(pytypes, m) { #endif #if defined(__cpp_inline_variables) + // Exercises const char* overload: m.attr_with_type_hint>("list_int") = py::list(); - m.attr_with_type_hint>("set_str") = py::set(); + // Exercises py::handle overload: + m.attr_with_type_hint>(py::str("set_str")) = py::set(); struct Empty {}; py::class_(m, "EmptyAnnotationClass");