Skip to content

Commit

Permalink
use getattr for automatic init
Browse files Browse the repository at this point in the history
  • Loading branch information
InvincibleRMC committed Dec 5, 2024
1 parent 6dff59f commit c8edd09
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class object_api : public pyobject_tag {
/// Get or set the object's docstring, i.e. ``obj.__doc__``.
str_attr_accessor doc() const;

/// Get or set the object's annotations, i.e. ``obj.__annotations``.
str_attr_accessor annotations() const;
/// Get or set the object's annotations, i.e. ``obj.__annotations__``.
object annotations() const;

/// Return the object's current reference count
ssize_t ref_count() const {
Expand Down Expand Up @@ -2566,12 +2566,9 @@ str_attr_accessor object_api<D>::doc() const {
}

template <typename D>
str_attr_accessor object_api<D>::annotations() const {
str_attr_accessor annotations_dict = attr("__annotations__");
// Create dict automatically
if (!isinstance<dict>(annotations_dict)) {
annotations_dict = dict();
}
// Always a dict
object object_api<D>::annotations() const {
dict annotations_dict = getattr(derived(), "__annotations__", dict());
return annotations_dict;
}

Expand Down

0 comments on commit c8edd09

Please sign in to comment.