Skip to content

Commit 2b89d7b

Browse files
committed
use a try except in _RequireAttrsABCMeta
1 parent 36d5ee7 commit 2b89d7b

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

adaptive/utils.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ class _RequireAttrsABCMeta(abc.ABCMeta):
7474
def __call__(self, *args, **kwargs):
7575
obj = super().__call__(*args, **kwargs)
7676
for name, type_ in obj.__annotations__.items():
77-
if not hasattr(obj, name):
78-
raise ValueError(f"Required attribute {name} not set in __init__.")
79-
elif not isinstance(getattr(obj, name), type_):
80-
raise TypeError(
81-
f"The attribute '{name}' is of {type_} instead of {type(getattr(obj, name))}"
82-
)
83-
77+
try:
78+
x = getattr(obj, name)
79+
if not isinstance(x, type_):
80+
raise TypeError(
81+
f"The attribute '{name}' is of {type_} instead of {type(x)}."
82+
)
83+
except AttributeError as e:
84+
raise e(f"Required attribute {name} not set in __init__.")
8485
return obj

0 commit comments

Comments
 (0)