try setattr

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

View File

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

View File

@ -1125,7 +1125,6 @@ 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: