1

Apparently when an AttributeError is raised from a property of a QObject then the traceback includes a different AttributeError instead namely one referring to the property itself.

Running the following code

import sys

from PyQt4 import QtCore


class MyObject(QtCore.QObject):
    def __init__(self, parent=None):
        super(MyObject, self).__init__(parent)

    @property
    def invalid_property(self):
        raise AttributeError('test')
        # raise TypeError('test')

    def invalid_method(self):
        raise AttributeError('test')

obj = MyObject()
obj.invalid_property
# obj.invalid_method()

gives

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    obj.invalid_property
AttributeError: 'MyObject' object has no attribute 'invalid_property'

That is the original AttributeError is replaced by a different one in the traceback.

I tried the same raising a TypeError which works just fine, as well as raising the AttributeError from a conventional method which works fine too.

If I inherit from object instead of QObject then the original AttributeError appears in the traceback too.

So what's happening here? Is a different AttributeError raised or is the original one's message just altered? And why does this happen? Is this a bug or intended behavior?


Related:

Community
  • 1
  • 1
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • Sounds like it might be falling back on a `__getattr__` method after your property fails to produce an attribute value. – user2357112 Feb 14 '17 at 22:45
  • 2
    This has got nothing to do with PyQt. It's normal Python behaviour for any class that implements `__getattr__` and raises `AttributeError` in a getter property. – ekhumoro Feb 15 '17 at 00:44

0 Answers0