Skip to content

Commit c5970fb

Browse files
committed
use type annotations
1 parent 0116e17 commit c5970fb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

adaptive/learner/base_learner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ class BaseLearner(metaclass=_RequireAttrsABCMeta):
8585
and returns a holoviews plot.
8686
"""
8787

88-
required_attributes = ["data", "npoints", "pending_points"]
88+
data: dict
89+
npoints: int
90+
pending_points: set
8991

9092
def tell(self, x, y):
9193
"""Tell the learner about a single value.

adaptive/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ def decorator(method):
7171

7272

7373
class _RequireAttrsABCMeta(abc.ABCMeta):
74-
required_attributes = []
75-
7674
def __call__(self, *args, **kwargs):
7775
obj = super().__call__(*args, **kwargs)
78-
for name in obj.required_attributes:
76+
for name, type_ in obj.__annotations__.items():
7977
if not hasattr(obj, name):
8078
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+
8184
return obj

0 commit comments

Comments
 (0)