Browse code

use type annotations

Bas Nijholt authored on 16/10/2019 20:41:24
Showing 2 changed files
... ...
@@ -85,7 +85,9 @@ class BaseLearner(metaclass=_RequireAttrsABCMeta):
85 85
     and returns a holoviews plot.
86 86
     """
87 87
 
88
-    required_attributes = ["data", "npoints", "pending_points"]
88
+    data: dict
89
+    npoints: int
90
+    pending_points: set
89 91
 
90 92
     def tell(self, x, y):
91 93
         """Tell the learner about a single value.
... ...
@@ -71,11 +71,14 @@ def copy_docstring_from(other):
71 71
 
72 72
 
73 73
 class _RequireAttrsABCMeta(abc.ABCMeta):
74
-    required_attributes = []
75
-
76 74
     def __call__(self, *args, **kwargs):
77 75
         obj = super().__call__(*args, **kwargs)
78
-        for name in obj.required_attributes:
76
+        for name, type_ in obj.__annotations__.items():
79 77
             if not hasattr(obj, name):
80 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
+
81 84
         return obj