From db9218e0df225a59151ee512870a7dafe9889937 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Fri, 3 May 2024 17:28:39 -0500 Subject: [PATCH 1/3] fix: ignore logging regular attr errs --- src/ape/utils/basemodel.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ape/utils/basemodel.py b/src/ape/utils/basemodel.py index 31f8b68837..e8c54f0c44 100644 --- a/src/ape/utils/basemodel.py +++ b/src/ape/utils/basemodel.py @@ -108,10 +108,12 @@ def only_raise_attribute_error(fn: Callable) -> Any: def wrapper(*args, **kwargs): try: return fn(*args, **kwargs) - except Exception as e: + except AttributeError: + raise # Don't modify or log normal already attr errors. + except Exception as err: # Wrap the exception in AttributeError logger.log_debug_stack_trace() - raise ApeAttributeError(f"{e}") from e + raise ApeAttributeError(f"{err}") from err return wrapper From c5ea941c1af3926fb24977fb5b4cfee9c382154c Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Fri, 3 May 2024 17:40:52 -0500 Subject: [PATCH 2/3] test: add test --- tests/functional/utils/test_basemodel.py | 30 +++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/functional/utils/test_basemodel.py b/tests/functional/utils/test_basemodel.py index 79c3d8909c..105c9d76ba 100644 --- a/tests/functional/utils/test_basemodel.py +++ b/tests/functional/utils/test_basemodel.py @@ -1,7 +1,8 @@ import pytest from ape.exceptions import ProviderNotConnectedError -from ape.utils.basemodel import ManagerAccessMixin +from ape.logging import logger +from ape.utils.basemodel import ManagerAccessMixin, only_raise_attribute_error class CustomClass(ManagerAccessMixin): @@ -22,3 +23,30 @@ def test_provider_not_active(networks, accessor): _ = accessor.provider finally: networks.active_provider = initial + + +def test_only_raise_attribute_error(mocker, ape_caplog): + spy = mocker.spy(logger, "log_debug_stack_trace") + + @only_raise_attribute_error + def fn(): + raise ValueError("foo bar error") + + with pytest.raises(AttributeError, match="foo bar error"): + fn() + + assert spy.call_count + + +def test_only_raise_attribute_error_when_already_raises(mocker, ape_caplog): + spy = mocker.spy(logger, "log_debug_stack_trace") + + @only_raise_attribute_error + def fn(): + raise AttributeError("foo bar error") + + with pytest.raises(AttributeError, match="foo bar error"): + fn() + + # Does not log because is already an attr err + assert not spy.call_count From f488ac338a2369d76e0b466f2f3014b94b81ba45 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Fri, 3 May 2024 17:42:14 -0500 Subject: [PATCH 3/3] docs: adj comment --- src/ape/utils/basemodel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ape/utils/basemodel.py b/src/ape/utils/basemodel.py index e8c54f0c44..3ca5c7bf8e 100644 --- a/src/ape/utils/basemodel.py +++ b/src/ape/utils/basemodel.py @@ -109,7 +109,7 @@ def wrapper(*args, **kwargs): try: return fn(*args, **kwargs) except AttributeError: - raise # Don't modify or log normal already attr errors. + raise # Don't modify or log attr errors. except Exception as err: # Wrap the exception in AttributeError logger.log_debug_stack_trace()