Skip to content

Commit

Permalink
Add SameAttributeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
durandtibo committed Jan 7, 2024
1 parent 6443519 commit 60efff8
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 201 deletions.
2 changes: 1 addition & 1 deletion src/coola/equality/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
]

from coola.equality.handlers.array import SameDTypeHandler
from coola.equality.handlers.attribute import SameAttributeHandler
from coola.equality.handlers.base import AbstractEqualityHandler, BaseEqualityHandler
from coola.equality.handlers.jax_ import JaxArrayEqualHandler
from coola.equality.handlers.mapping import (
Expand All @@ -37,6 +36,7 @@
from coola.equality.handlers.native import (
FalseHandler,
ObjectEqualHandler,
SameAttributeHandler,
SameLengthHandler,
SameObjectHandler,
SameTypeHandler,
Expand Down
72 changes: 0 additions & 72 deletions src/coola/equality/handlers/attribute.py

This file was deleted.

56 changes: 56 additions & 0 deletions src/coola/equality/handlers/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__all__ = [
"FalseHandler",
"ObjectEqualHandler",
"SameAttributeHandler",
"SameLengthHandler",
"SameObjectHandler",
"SameTypeHandler",
Expand All @@ -15,6 +16,7 @@
from typing import TYPE_CHECKING, Any

from coola.equality.handlers.base import AbstractEqualityHandler, BaseEqualityHandler
from coola.utils import repr_indent, repr_mapping

if TYPE_CHECKING:
from collections.abc import Sized
Expand Down Expand Up @@ -150,6 +152,60 @@ def set_next_handler(self, handler: BaseEqualityHandler) -> None:
pass # Do nothing because the next handler is never called.


class SameAttributeHandler(AbstractEqualityHandler):
r"""Check if the two objects have the same attribute.
This handler returns ``False`` if the two objects have different
attributes, otherwise it passes the inputs to the next handler.
The objects must have the attribute.
Example usage:
```pycon
>>> import numpy as np
>>> from coola.equality import EqualityConfig
>>> from coola.equality.handlers import SameAttributeHandler, TrueHandler
>>> from coola.testers import EqualityTester
>>> config = EqualityConfig(tester=EqualityTester())
>>> handler = SameAttributeHandler(name="shape", next_handler=TrueHandler())
>>> handler.handle(np.ones((2, 3)), np.ones((2, 3)), config)
True
>>> handler.handle(np.ones((2, 3)), np.ones((3, 2)), config)
False
```
"""

def __init__(self, name: str, next_handler: BaseEqualityHandler | None = None) -> None:
super().__init__(next_handler=next_handler)
self._name = name

def __eq__(self, other: object) -> bool:
if not isinstance(other, self.__class__):
return False
return self.name == other.name

def __repr__(self) -> str:
args = repr_indent(repr_mapping({"name": self._name, "next_handler": self._next_handler}))
return f"{self.__class__.__qualname__}(\n {args}\n)"

def __str__(self) -> str:
return f"{self.__class__.__qualname__}(name={self._name})"

@property
def name(self) -> str:
return self._name

def handle(self, object1: Any, object2: Any, config: EqualityConfig) -> bool:
value1 = getattr(object1, self._name)
value2 = getattr(object2, self._name)
if not config.tester.equal(value1, value2, config.show_difference):
if config.show_difference:
logger.info(f"objects have different {self._name}: {value1} vs {value2}")
return False
return self._handle_next(object1=object1, object2=object2, config=config)


class SameLengthHandler(AbstractEqualityHandler):
r"""Check if the two objects have the same length.
Expand Down
128 changes: 0 additions & 128 deletions tests/unit/equality/handlers/test_attribute.py

This file was deleted.

Loading

0 comments on commit 60efff8

Please sign in to comment.