Skip to content

Commit

Permalink
refactor: use @raises_not_implemented() instead of raise statemen…
Browse files Browse the repository at this point in the history
…ts in `BaseManager` ipython methods (#2069)
  • Loading branch information
antazoey authored May 7, 2024
1 parent 534952b commit 4c346d3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ape/managers/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from ape.utils import ManagerAccessMixin
from ape.utils import ManagerAccessMixin, raises_not_implemented


class BaseManager(ManagerAccessMixin):
"""
Base manager that allows us to add other IPython integration features
"""

@raises_not_implemented
def _repr_mimebundle_(self, include=None, exclude=None):
# This works better than AttributeError for Ape.
raise NotImplementedError("This manager does not implement '_repr_mimebundle_'.")
pass

@raises_not_implemented
def _ipython_display_(self, include=None, exclude=None):
# This works better than AttributeError for Ape.
raise NotImplementedError("This manager does not implement '_ipython_display_'.")
pass
25 changes: 25 additions & 0 deletions tests/functional/test_base_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from ape.exceptions import APINotImplementedError
from ape.managers.base import BaseManager


@pytest.fixture(scope="module")
def manager():
class MyManager(BaseManager):
pass

return MyManager()


@pytest.mark.parametrize("fn_name", ("_repr_mimebundle_", "_ipython_display_"))
def test_ipython_integration_defaults(manager, fn_name):
"""
Test default behavior for IPython integration methods.
The base-manager short-circuits to NotImplementedError to avoid
dealing with any custom `__getattr__` logic entirely. This prevents
side-effects such as unnecessary compiling in the ProjectManager.
"""
with pytest.raises(APINotImplementedError):
fn = getattr(manager, fn_name)
fn()

0 comments on commit 4c346d3

Please sign in to comment.