diff --git a/ops/model.py b/ops/model.py index 707ef42ba..6405e7080 100644 --- a/ops/model.py +++ b/ops/model.py @@ -1907,6 +1907,9 @@ def __init__(self, message: str = ''): raise TypeError('cannot instantiate a base class') self.message = message + def __init_subclass__(cls): + StatusBase.register(cls) + def __eq__(self, other: 'StatusBase') -> bool: if not isinstance(self, type(other)): return False @@ -1965,7 +1968,6 @@ def _get_highest_priority(cls, statuses: 'List[StatusBase]') -> 'StatusBase': return max(statuses, key=lambda status: cls._priorities.get(status.name, 0)) -@StatusBase.register class UnknownStatus(StatusBase): """The unit status is unknown. @@ -1986,7 +1988,6 @@ def __repr__(self): return 'UnknownStatus()' -@StatusBase.register class ErrorStatus(StatusBase): """The unit status is error. @@ -2000,7 +2001,6 @@ class ErrorStatus(StatusBase): name = 'error' -@StatusBase.register class ActiveStatus(StatusBase): """The unit or application is ready and active. @@ -2016,7 +2016,6 @@ def __init__(self, message: str = ''): super().__init__(message) -@StatusBase.register class BlockedStatus(StatusBase): """The unit or application requires manual intervention. @@ -2027,7 +2026,6 @@ class BlockedStatus(StatusBase): name = 'blocked' -@StatusBase.register class MaintenanceStatus(StatusBase): """The unit or application is performing maintenance tasks. @@ -2041,7 +2039,6 @@ class MaintenanceStatus(StatusBase): name = 'maintenance' -@StatusBase.register class WaitingStatus(StatusBase): """The unit or application is waiting on a charm it's integrated with. diff --git a/test/test_model.py b/test/test_model.py index 0eadd4d2c..7af32262d 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -820,11 +820,15 @@ def test_base_status_instance_raises(self): with pytest.raises(TypeError): ops.StatusBase('test') - class NoNameStatus(ops.StatusBase): - pass + with pytest.raises(TypeError): + # TypeError due to missing `name` attribute + class NoNameStatus(ops.StatusBase): # pyright: ignore[reportUnusedClass] + pass with pytest.raises(TypeError): - ops.StatusBase.register(NoNameStatus) + # TypeError due to non str type `name` attribute + class NonStringNameStatus(ops.StatusBase): # pyright: ignore[reportUnusedClass] + name = None # pyright: ignore[reportAssignmentType] def test_status_repr(self): test_cases = { diff --git a/test/test_testing.py b/test/test_testing.py index 4c88f232b..965ebc894 100644 --- a/test/test_testing.py +++ b/test/test_testing.py @@ -3232,6 +3232,9 @@ def test_get_filesystem_root(self): def test_evaluate_status(self): class TestCharm(ops.CharmBase): + app_status_to_add: ops.StatusBase + unit_status_to_add: ops.StatusBase + def __init__(self, framework: ops.Framework): super().__init__(framework) self.framework.observe(self.on.collect_app_status, self._on_collect_app_status)