From 115648eb37ae2606837ca8b53fd277ed847b3219 Mon Sep 17 00:00:00 2001 From: antazoey Date: Thu, 31 Oct 2024 10:46:00 -0500 Subject: [PATCH] fix: more helpful traceback for `__getattr__` on `project` (#2357) --- src/ape/managers/project.py | 6 ++++-- tests/functional/test_project.py | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ape/managers/project.py b/src/ape/managers/project.py index f84e19e67a..c900e8fdb6 100644 --- a/src/ape/managers/project.py +++ b/src/ape/managers/project.py @@ -2216,8 +2216,10 @@ def __getattr__(self, item: str) -> Any: "missing compilers for extensions: " + f'{", ".join(sorted(missing_exts))}?' ) - err.args = (message,) - raise # The same exception (keep the stack the same height). + # NOTE: Purposely discard the stack-trace and raise a new exception. + # This shows a better stack-trace to the user (rather than weird + # BaseModel internals). + raise AttributeError(message) @property def _contract_sources(self) -> list[ContractSource]: diff --git a/tests/functional/test_project.py b/tests/functional/test_project.py index 256fdb841e..e36855cc3f 100644 --- a/tests/functional/test_project.py +++ b/tests/functional/test_project.py @@ -209,9 +209,16 @@ def test_getattr(tmp_project): def test_getattr_not_exists(tmp_project): - with pytest.raises(AttributeError): + expected = ( + r"'LocalProject' object has no attribute 'nope'\. Also checked extra\(s\) 'contracts'\." + ) + with pytest.raises(AttributeError, match=expected) as err: _ = tmp_project.nope + # Was the case where the last entry was from Ape's basemodel stuff. + # Now, it points at the project manager last. + assert "ape/managers/project.py:" in repr(err.traceback[-1]) + def test_getattr_detects_changes(tmp_project): source_id = tmp_project.Other.contract_type.source_id