Skip to content

Commit

Permalink
add #if 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
InvincibleRMC committed Dec 6, 2024
1 parent e5235f0 commit 8da0ce0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
16 changes: 11 additions & 5 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2569,12 +2569,18 @@ template <typename D>
// Always a dict
// https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
object object_api<D>::annotations() const {
if (isinstance<type>(derived())){
return getattr(getattr(derived(), "__dict__"), "__annotations__", dict());
}
else{
// Python 3.8, 3.9
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9
if (isinstance<type>(derived())){
return getattr(derived(), "__dict__").get("__annotations__", dict());
}
else{
return getattr(derived(), "__annotations__", dict());
}
// Python 3.10+
#else
return getattr(derived(), "__annotations__", dict());
}
#endif
}

template <typename D>
Expand Down
8 changes: 5 additions & 3 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,12 @@ def test_dict_ranges(tested_dict, expected):
assert m.dict_iterator_default_initialization()
assert m.transform_dict_plus_one(tested_dict) == expected


# https://docs.python.org/3/howto/annotations.html#accessing-the-annotations-dict-of-an-object-in-python-3-9-and-older
def get_annotations_helper(o):
print(help(o))
return getattr(o, "__annotations__", None)
if isinstance(o, type):
return o.__dict__.get('__annotations__', {})
else:
return getattr(o, '__annotations__', {})


def test_module_attribute_types() -> None:
Expand Down

0 comments on commit 8da0ce0

Please sign in to comment.