Skip to content

Commit

Permalink
Switch metaclass to __init_subclass__
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Jun 28, 2024
1 parent 1bc2cb9 commit 7e11695
Showing 1 changed file with 13 additions and 38 deletions.
51 changes: 13 additions & 38 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,28 +1451,7 @@ def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)


class _CommonMeta(type):
"""
A meta class for all :py:class:`Common` classes.
Takes care of properly resetting :py:attr:`Common.cli_invocation` attribute
that cannot be shared among classes.
"""

def __init__(cls, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

# TODO: repeat type annotation from `Common` - IIUIC, `cls` should be
# the class being created, in our case that would be a subclass of
# `Common`. For some reason, mypy is uncapable of detecting annotation
# of this attribute in `Common`, and infers its type is `None` because
# of the assignment below. That's incomplete, and leads to mypy warning
# about assignments of `CliInvocation` instances to this attribute.
# Repeating the annotation silences mypy, giving it better picture.
cls.cli_invocation: Optional[tmt.cli.CliInvocation] = None


class Common(_CommonBase, metaclass=_CommonMeta):
class Common(_CommonBase):
"""
Common shared stuff
Expand Down Expand Up @@ -1608,6 +1587,13 @@ def __str__(self) -> str:
# like --how or --dry, may affect step data from fmf or even spawn new phases.
cli_invocation: Optional['tmt.cli.CliInvocation'] = None

def __init_subclass__(cls) -> None:
"""
Take care of properly resetting :py:attr:`Common.cli_invocation` attribute
that cannot be shared among classes.
"""
cls.cli_invocation = None

@classmethod
def store_cli_invocation(
cls,
Expand Down Expand Up @@ -2185,27 +2171,16 @@ def clone_dirpath(self) -> Path:
return self._clone_dirpath


class _MultiInvokableCommonMeta(_CommonMeta):
"""
A meta class for all :py:class:`Common` classes.
Takes care of properly resetting :py:attr:`Common.cli_invocation` attribute
that cannot be shared among classes.
"""

# N805: ruff does not recognize this as a metaclass, `cls` is correct
def __init__(cls, *args: Any, **kwargs: Any) -> None: # noqa: N805
super().__init__(*args, **kwargs)

cls.cli_invocations: list[tmt.cli.CliInvocation] = []


class MultiInvokableCommon(Common, metaclass=_MultiInvokableCommonMeta):
class MultiInvokableCommon(Common):
cli_invocations: list['tmt.cli.CliInvocation']

def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)

def __init_subclass__(cls) -> None:
super().__init_subclass__()
cls.cli_invocations = []

@classmethod
def store_cli_invocation(
cls,
Expand Down

0 comments on commit 7e11695

Please sign in to comment.