Skip to content

Usage of both @property and __getattr__ can lead to obscure and hard to debug errors #2108

Closed
@skrivanos

Description

@skrivanos

Encountered this error when accessing request.DATA in a view:

  File "views.py", line 146, in put
    if request.DATA is not None:
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/request.py", line 453, in __getattr__
    return getattr(self._request, attr)
AttributeError: 'WSGIRequest' object has no attribute 'DATA'

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:

AttributeError: 'TemporaryFileUploadHandler' object has no attribute 'file'

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions