mirror of
https://github.com/pybind/pybind11.git
synced 2025-01-19 01:15:52 +00:00
use hasattr
This commit is contained in:
parent
e84df95bf2
commit
d660177409
@ -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());
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user