Skip to content

Commit

Permalink
refactor: deprecate StatusBase.register decorator (canonical#1384)
Browse files Browse the repository at this point in the history
As `StatusBase.__init_subclass__` is now used to automatically register
status classes by name, and the old `StatusBase.register` decorator
shouldn't have been public in the first place, it is now deprecated.
  • Loading branch information
james-garner-canonical authored Sep 23, 2024
1 parent 4bb92dd commit 98a4e4f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ def __init__(self, message: str = ''):
self.message = message

def __init_subclass__(cls):
StatusBase.register(cls)
StatusBase._register(cls)

def __eq__(self, other: 'StatusBase') -> bool:
if not isinstance(self, type(other)):
Expand Down Expand Up @@ -1941,14 +1941,21 @@ def from_name(cls, name: str, message: str):

@classmethod
def register(cls, child: Type['StatusBase']):
"""Register a Status for the child's name."""
""".. deprecated:: 2.17.0 Deprecated - this was for internal use only."""
warnings.warn(
'StatusBase.register is for internal use only', DeprecationWarning, stacklevel=2
)
cls._register(child)
return child

@classmethod
def _register(cls, child: Type['StatusBase']) -> None:
if not (hasattr(child, 'name') and isinstance(child.name, str)):
raise TypeError(
f"Can't register StatusBase subclass {child}: ",
'missing required `name: str` class attribute',
)
cls._statuses[child.name] = child
return child

_priorities = {
'error': 5,
Expand Down
4 changes: 4 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,10 @@ class NoNameStatus(ops.StatusBase): # pyright: ignore[reportUnusedClass]
class NonStringNameStatus(ops.StatusBase): # pyright: ignore[reportUnusedClass]
name = None # pyright: ignore[reportAssignmentType]

def test_base_status_register_is_deprecated(self):
with pytest.deprecated_call():
ops.StatusBase.register(ops.ActiveStatus)

def test_status_repr(self):
test_cases = {
"ActiveStatus('Seashell')": ops.ActiveStatus('Seashell'),
Expand Down

0 comments on commit 98a4e4f

Please sign in to comment.