У меня проблема с AttributeErrors
, поднятым в @property
в сочетании с __getattr__()
в python:
Пример кода:
>>> 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'
В моем случае это очень вводящее в заблуждение сообщение об ошибке, потому что оно скрывает тот факт, что deeply_nested_factory_fn()
содержит ошибку.
Основываясь на идее в ответе Тадга Макдональда-Дженсена, мое лучшее решение на данный момент заключается в следующем. Буду очень признателен за любые подсказки о том, как избавиться от префикса __main__.
к AttributeError
и ссылки на attributeErrorCatcher
в трассировке.
>>> 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'
__qualname__ = "AttributeError"
в определении класса, чтобы удалить часть__main__
, но поверьте мне, вы не хотите, чтобы ошибка просто говорила AttributeError, потому что тогда вы рискуете быть сбитым с толку, почему, черт возьми,except AttributeError
не поймал ошибка атрибута. - person Tadhg McDonald-Jensen   schedule 13.04.2016