diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 7ba991e06..d7482927b 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -2571,7 +2571,7 @@ template object object_api::annotations() const { // Python 3.8, 3.9 #if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION <= 9 - if (isinstance(derived())) { + if (hasattr(derived(), "__dict__")) { return getattr(getattr(derived(), "__dict__"), "__annotations__", dict()); } else { return getattr(derived(), "__annotations__", dict()); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 8545fac5c..332f2125e 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -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: