-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Usage of both @property and __getattr__ can lead to obscure and hard to debug errors #2108
Comments
Yes, definitely the same error. Pretty sure the same solution applied in #1938 would fix it. Probably needs fixing in all getters/setters in classes with custom properties and getattr defined. Another option might be to override |
Any update on this issue? |
Not yet. Plenty of other things going on, but anyone's welcome to dig into this and come up with clear proposal/example pull request etc. |
Dang, I'm having the same problem but I can't trace it. |
One route someone to start on this would be to demonstrate the most minimal possible python example that demonstates the issue (ie forget about REST framework at all to start with) That'd give us a much better point to work towards a fix from, as it'd narrow things down to the core issue. |
class Test(object):
@property
def foo(self):
# This will raise an AttributeError, after which
# __getattr__ will be used as a fallback.
return self.doesntexist
def __getattr__(self, name):
raise AttributeError('No attribute ' + name)
t = Test()
t.foo The output will be
As you can see, the original exception and traceback is lost (from trying to access self.doesntexist). |
Now resolved. Thanks everyone for your input on this! :) |
I finally found my problem; I was using |
Encountered this error when accessing request.DATA in a view:
This was mind-boggling. I couldn't understand why
__getattr__
was ever called on the request object, since a property called DATA already exists (confirmed this via dir(request) before accessing DATA), and as we all know, getattr is only used for undefined properties.After an intense debugging session it turned out that this is caused by a "feature" in python. This may be common knowledge, but it definitely was new news to me: if a property getter raises an AttributeError, python (
__getattribute__
) falls back to using__getattr__
. The issue is that the traceback is lost, which makes it extremely hard to debug, since the actual exception raised isn't shown anywhere. So, the actual error was:Further explanation/discussion: https://groups.google.com/forum/#!topic/comp.lang.python/BZf-d0rLP8U
I'm not sure if this is an issue that you can/want to address, but I wanted to make people aware of it since it was a pain to track down and others might stumble into the same situation.
The text was updated successfully, but these errors were encountered: