Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: deprecate StatusBase.register decorator #1384

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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():
james-garner-canonical marked this conversation as resolved.
Show resolved Hide resolved
ops.StatusBase.register(ops.ActiveStatus)

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