-
Notifications
You must be signed in to change notification settings - Fork 952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow snooping on all methods of a class #150
Comments
This is a cute idea. My priority is to keep the interface as simple as possible, so if you'd like me to merge it, I'd like you to modify it so it's a decorator on the class rather than a metaclass. Submit a PR like that, with tests and documentation, and I'll likely merge it. |
I considered doing this for snoop, I just wanted to hear other people's thoughts on it first: alexmojaki/snoop#2 The main problem with the kind of implementation above (metaclass or not) is that decorated methods in the class won't be traced correctly. If you have a traditional decorator: def my_decorator(f):
def wrapper(*args, **kwargs):
...
return wrapper And a method: @my_decorator
def my_method then the metaclass above will trace |
OK @cool-RR, I'll give it a shot. @alexmojaki That's an interesting point, not sure how pysnooper interacts with other decorators in the first place. |
PySnooper does nothing to specially handle or be aware of other decorators. If you do this: @snoop()
@my_decorator
def my_method then you will snoop @my_decorator
@snoop()
def my_method In fact as they consider adding
We could perhaps add some code to address this, e.g. to show a warning if The problem is a bit different for some other decorators like builtins, e.g: @snoop()
@property
def my_method Then currently you get an error like
which again users can probably diagnose fairly easily. The same thing happens with If you decorate an entire class though, the situation becomes quite different. It's much more likely that at least some of the methods of a class have decorators compared to single function having one. The user is likely to forget that those decorators are there (especially in long classes with many methods) and realise the implications, even after seeing strange debugging output. If they do see the problem, there's no easy way to fix it like before. Your implementation would need to specially handle each unusual decorator like |
Another problem: import pysnooper
@pysnooper.snoop()
class A:
@pysnooper.snoop()
def foo(self):
pass
A().foo() Output:
|
I didn't find a way to easily snoop on all the methods of a class without adding the decorator to every single method. Below is an outline of a way to do it with a metaclass. There was an issue overwriting the magic/dunder methods (
AttributeError: type object 'SnoopedMetaClass' has no attribute '__code__'
), but for everything else it works fine.The text was updated successfully, but these errors were encountered: