remove asserts from instrumentation
This commit is contained in:
parent
e9fba7b064
commit
844dacf989
@ -13,8 +13,10 @@ class Instrumentation(Generic[IType]):
|
|||||||
_wrap: Callable
|
_wrap: Callable
|
||||||
|
|
||||||
def __init__(self, target, methodname: str):
|
def __init__(self, target, methodname: str):
|
||||||
assert isinstance(methodname, str)
|
if not isinstance(methodname, str):
|
||||||
assert callable(getattr(target, methodname))
|
raise TypeError('methodname must be str')
|
||||||
|
if not callable(getattr(target, methodname)):
|
||||||
|
raise TypeError('target.methodname must be callable')
|
||||||
self.target = target
|
self.target = target
|
||||||
self.methodname = methodname
|
self.methodname = methodname
|
||||||
|
|
||||||
@ -22,11 +24,13 @@ class Instrumentation(Generic[IType]):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def __enter__(self: IType) -> IType:
|
def __enter__(self: IType) -> IType:
|
||||||
assert isinstance(self, Instrumentation)
|
if not isinstance(self, Instrumentation):
|
||||||
assert not hasattr(self, '_method')
|
raise TypeError
|
||||||
assert not hasattr(self, '_wrap')
|
if hasattr(self, '_method') or hasattr(self, '_wrap'):
|
||||||
|
raise RuntimeError
|
||||||
method = getattr(self.target, self.methodname)
|
method = getattr(self.target, self.methodname)
|
||||||
assert callable(method)
|
if not callable(method):
|
||||||
|
raise TypeError
|
||||||
self._method = method
|
self._method = method
|
||||||
|
|
||||||
@functools.wraps(method)
|
@functools.wraps(method)
|
||||||
@ -56,11 +60,13 @@ class Instrumentation(Generic[IType]):
|
|||||||
self.deinstrument()
|
self.deinstrument()
|
||||||
|
|
||||||
def enter(self: IType, stack: ExitStack) -> IType:
|
def enter(self: IType, stack: ExitStack) -> IType:
|
||||||
assert isinstance(self, Instrumentation)
|
if not isinstance(self, Instrumentation):
|
||||||
|
raise TypeError
|
||||||
return stack.enter_context(self)
|
return stack.enter_context(self)
|
||||||
|
|
||||||
def enter_conditional(self: IType, stack: ExitStack) -> IType | None:
|
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):
|
if hasattr(self.target, self.methodname):
|
||||||
return self.enter(stack)
|
return self.enter(stack)
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user