mirror of
https://github.com/pybind/pybind11.git
synced 2025-01-19 01:15:52 +00:00
add handle support to attr_with_type_hint
This commit is contained in:
parent
981e0a3c83
commit
7f7b70b943
@ -1368,6 +1368,22 @@ object object_or_cast(T &&o) {
|
||||
|
||||
// Declared in pytypes.h:
|
||||
// Written here so make_caster<T> can be used
|
||||
template <typename D>
|
||||
template <typename T>
|
||||
obj_attr_accessor object_api<D>::attr_with_type_hint(handle key) const {
|
||||
#if !defined(__cpp_inline_variables)
|
||||
static_assert(!std::is_same<T, T>::value,
|
||||
"C++17 feature __cpp_inline_variables not available: "
|
||||
"https://en.cppreference.com/w/cpp/language/static#Static_data_members");
|
||||
#endif
|
||||
object ann = annotations();
|
||||
if (ann.contains(key)) {
|
||||
throw std::runtime_error("__annotations__[\"" + std::string(key) + "\"] was set already.");
|
||||
}
|
||||
ann[key] = make_caster<T>::name.text;
|
||||
return {derived(), reinterpret_borrow<object>(key)};
|
||||
}
|
||||
|
||||
template <typename D>
|
||||
template <typename T>
|
||||
str_attr_accessor object_api<D>::attr_with_type_hint(const char *key) const {
|
||||
|
@ -114,6 +114,9 @@ public:
|
||||
str_attr_accessor attr(const char *key) const;
|
||||
|
||||
// attr_with_type_hint is implemented in cast.h:
|
||||
template <typename T>
|
||||
obj_attr_accessor attr_with_type_hint(handle key) const;
|
||||
|
||||
template <typename T>
|
||||
str_attr_accessor attr_with_type_hint(const char *key) const;
|
||||
|
||||
|
@ -1160,7 +1160,6 @@ def test_class_attribute_types() -> None:
|
||||
def test_redeclaration_attr_with_type_hint() -> None:
|
||||
obj = m.Instance()
|
||||
m.attr_with_type_hint_float_x(obj)
|
||||
help(obj)
|
||||
assert get_annotations_helper(obj)["x"] == "float"
|
||||
with pytest.raises(
|
||||
RuntimeError, match=r'^__annotations__\["x"\] was set already\.$'
|
||||
|
Loading…
Reference in New Issue
Block a user