diff --git a/rainbowadn/instrument/instrumentation.py b/rainbowadn/instrument/instrumentation.py index bb8be28..fe65171 100644 --- a/rainbowadn/instrument/instrumentation.py +++ b/rainbowadn/instrument/instrumentation.py @@ -13,8 +13,10 @@ class Instrumentation(Generic[IType]): _wrap: Callable def __init__(self, target, methodname: str): - assert isinstance(methodname, str) - assert callable(getattr(target, methodname)) + if not isinstance(methodname, str): + raise TypeError('methodname must be str') + if not callable(getattr(target, methodname)): + raise TypeError('target.methodname must be callable') self.target = target self.methodname = methodname @@ -22,11 +24,13 @@ class Instrumentation(Generic[IType]): raise NotImplementedError def __enter__(self: IType) -> IType: - assert isinstance(self, Instrumentation) - assert not hasattr(self, '_method') - assert not hasattr(self, '_wrap') + if not isinstance(self, Instrumentation): + raise TypeError + if hasattr(self, '_method') or hasattr(self, '_wrap'): + raise RuntimeError method = getattr(self.target, self.methodname) - assert callable(method) + if not callable(method): + raise TypeError self._method = method @functools.wraps(method) @@ -56,11 +60,13 @@ class Instrumentation(Generic[IType]): self.deinstrument() def enter(self: IType, stack: ExitStack) -> IType: - assert isinstance(self, Instrumentation) + if not isinstance(self, Instrumentation): + raise TypeError return stack.enter_context(self) def enter_conditional(self: IType, stack: ExitStack) -> IType | None: - assert isinstance(self, Instrumentation) + if not isinstance(self, Instrumentation): + raise TypeError if hasattr(self.target, self.methodname): return self.enter(stack) else: