nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: canonical/operator
Pull request #1384 (2024-09-22 22:05:51)

refactor: deprecate StatusBase.register decorator
As `StatusBase.__init_subclass__` is now used to automatically register status classes by name, and the old `StatusBase.register` decorator (1) shouldn't have been public, (2) shouldn't have been used externally anyway, (3) doesn't appear to be used in charms currently, and (4) isn't needed internally, let's deprecate it.

See also discussion on #1380.


Patch:
diff --git a/ops/model.py b/ops/model.py
index 6405e70..d03c4b5 100644
--- a/ops/model.py
+++ b/ops/model.py
@@ -1908,7 +1908,7 @@ class StatusBase:
         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)):
@@ -1941,14 +1941,21 @@ class StatusBase:
 
     @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,


Test patch:
diff --git a/test/test_model.py b/test/test_model.py
index 1f0b639..015dbc0 100644
--- a/test/test_model.py
+++ b/test/test_model.py
@@ -830,6 +830,10 @@ class TestModel:
             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'),