Skip to content

Commit

Permalink
Updated minxins File & Added corresponding Tests as well tests_minxin
Browse files Browse the repository at this point in the history
  • Loading branch information
RISHIKESHk07 committed Sep 11, 2024
1 parent cfc9e1f commit dd6163a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
40 changes: 40 additions & 0 deletions xcov19/tests/tests_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from xcov19.utils.mixins import InterfaceProtocolCheckMixin


class BaseClass:
@classmethod
def method_with_params(cls, param1: int, param2: str) -> None:
pass


def test_incorrect_implementation_missing_method():
# Define IncorrectImplementation inside the test function
try:

class IncorrectImplementation(BaseClass, InterfaceProtocolCheckMixin):
"""Does not implement method_with_params"""

pass

IncorrectImplementation()

except NotImplementedError as exec:
assert (
str(exec)
== "The method 'method_with_params' is inherited from the parent class 'BaseClass' and not overridden/declared."
)


def test_correct_implementation():
class CorrectImplementation(BaseClass, InterfaceProtocolCheckMixin):
@classmethod
def method_with_params(cls, param1: int, param2: str) -> None:
pass

try:
instance = CorrectImplementation()
instance.method_with_params(1, "test")
assert True
except Exception as e:
pytest.fail(f"CorrectImplementation raised an exception: {e}")
21 changes: 18 additions & 3 deletions xcov19/utils/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import operator
from typing import Tuple, Any, TypeVar, get_type_hints


ClassNameAttrGetter = operator.attrgetter("__name__")
BoundAttrGetter = operator.attrgetter("__bound__")

Expand Down Expand Up @@ -53,8 +52,24 @@ def __init_subclass__(cls, **kwargs):
if not method_name.startswith("__")
):
# TODO: Raise if either classes don't have the method declared.
cls_method = getattr(parent_class, defined_method)
subclass_method = getattr(cls, defined_method)

try:
# Get the method from both the parent and subclass
cls_method = getattr(parent_class, defined_method)
subclass_method = getattr(cls, defined_method)

# Ensure the method is declared/overridden in the subclass and not just inherited
if defined_method in cls.__dict__:
pass
else:
raise NotImplementedError(
f"The method '{defined_method}' is inherited from the parent class '{parent_class.__name__}' and not overridden/declared."
)
except AttributeError:
raise NotImplementedError(
f"Method '{defined_method}' not found in parent class."
)

cls_method_params: dict = get_type_hints(cls_method)
subclass_method_params: dict = get_type_hints(subclass_method)
if len(cls_method_params) != len(subclass_method_params):
Expand Down

0 comments on commit dd6163a

Please sign in to comment.