use hasattr

This commit is contained in:
Michael Carlstrom 2024-12-05 20:09:33 -05:00
parent e84df95bf2
commit d660177409
2 changed files with 3 additions and 2 deletions

View File

@ -2571,7 +2571,7 @@ template <typename D>
object object_api<D>::annotations() const {
// Python 3.8, 3.9
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9
if (isinstance<type>(derived())) {
if (hasattr(derived(), "__dict__")) {
return getattr(getattr(derived(), "__dict__"), "__annotations__", dict());
} else {
return getattr(derived(), "__annotations__", dict());

View File

@ -1105,7 +1105,7 @@ def test_dict_ranges(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):
dir(o)
print(dir(o))
if isinstance(o, type):
return o.__dict__.get("__annotations__", {})
return getattr(o, "__annotations__", {})
@ -1125,6 +1125,7 @@ def test_class_attribute_types() -> None:
assert empty_annotations == {}
assert annotations["x"] == "float"
assert annotations["dict_str_int"] == "dict[str, int]"
assert False
def test_final_annotation() -> None: