Ich habe ein Problem mit AttributeErrors
in einem @property
in Kombination mit __getattr__()
in Python angehoben:AttributeErrors: unerwünschte Wechselwirkung zwischen @property und __getattr__
Beispielcode:
>>> def deeply_nested_factory_fn():
... a = 2
... return a.invalid_attr
...
>>> class Test(object):
... def __getattr__(self, name):
... if name == 'abc':
... return 'abc'
... raise AttributeError("'Test' object has no attribute '%s'" % name)
... @property
... def my_prop(self):
... return deeply_nested_factory_fn()
...
>>> test = Test()
>>> test.my_prop
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in __getattr__
AttributeError: 'Test' object has no attribute 'my_prop'
In meinem Fall ist dies ein sehr irreführende Fehlermeldung, weil es die Tatsache versteckt, dass deeply_nested_factory_fn()
einen Fehler hat.
Basierend auf der Idee in Tadhg McDonald-Jensen Antwort, meine zur Zeit beste Lösung ist die folgende. Irgendwelche Hinweise darauf, wie das __main__.
Präfix zu AttributeError
und der Verweis auf attributeErrorCatcher
im Traceback loszuwerden ist, würden sehr geschätzt werden.
>>> def catchAttributeErrors(func):
... AttributeError_org = AttributeError
... def attributeErrorCatcher(*args, **kwargs):
... try:
... return func(*args, **kwargs)
... except AttributeError_org as e:
... import sys
... class AttributeError(Exception):
... pass
... etype, value, tb = sys.exc_info()
... raise AttributeError(e).with_traceback(tb.tb_next) from None
... return attributeErrorCatcher
...
>>> def deeply_nested_factory_fn():
... a = 2
... return a.invalid_attr
...
>>> class Test(object):
... def __getattr__(self, name):
... if name == 'abc':
... # computing come other attributes
... return 'abc'
... raise AttributeError("'Test' object has no attribute '%s'" % name)
... @property
... @catchAttributeErrors
... def my_prop(self):
... return deeply_nested_factory_fn()
...
>>> class Test1(object):
... def __init__(self):
... test = Test()
... test.my_prop
...
>>> test1 = Test1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
File "<stdin>", line 11, in attributeErrorCatcher
File "<stdin>", line 10, in my_prop
File "<stdin>", line 3, in deeply_nested_factory_fn
__main__.AttributeError: 'int' object has no attribute 'invalid_attr'
Sie müssten '__qualname__ =" AttributeError "' in der Klassendefinition tun, um den '__main__' Teil zu entfernen, aber vertrauen Sie mir ** Sie wollen ** den Fehler nicht einfach nur AttributeError sagen, denn dann riskieren Sie, verwirrt zu werden warum das heck 'except AttributeError' kein AttributError gefunden hat. –