Skip to content

Commit

Permalink
fix: more helpful traceback for __getattr__ on project (#2357)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Oct 31, 2024
1 parent a6ddfff commit 115648e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
9 changes: 8 additions & 1 deletion tests/functional/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 115648e

Please sign in to comment.