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

Implemented __eq__ function for Kovats and CubicSpline via ComputationMethod class #52

Merged
merged 6 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Added `__eq__` to `ComputationMethod` class and subclasses [#52](https://github.com/RECETOX/RIAssigner/pull/52)
### Changed
### Removed
## [0.1.0] - 2021-07-12
Expand Down
3 changes: 3 additions & 0 deletions RIAssigner/compute/ComputationMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ def _check_data_args(self, query, reference):
"""
assert query is not None, "Query data is 'None'."
assert reference is not None, "Reference data is 'None'."

def __eq__(self, o: object) -> bool:
return isinstance(o, type(self))
Copy link
Contributor

@xtrojak xtrojak Jul 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note to consider - if in the future you will possibly use nested inheritance
(e.g. create a new Computation Method XY from Kovats, where you, for example, change only one of its methods).

class XY(Kovats):
    ...

Then, this kind of equality check Kovats() == XY() returns True, because isinstance considers also inheritance. Is that desired behavior? (See this SO post for more detials)

A way to change this is to explicitelly compare their types, i.e.

def __eq__(self, o: object) -> bool:
        return type(o) == type(self)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment! My linter complains about the type comparison with type(o) == type(self) and I am aware of the subclassing issue.

I don't think there will be computation methods derived from any of the derived classes, but this is good to know. I might violate the linter and change the type check to this one which seems to be stricter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the linter's output?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using type() instead of isinstance() for a typecheck.

2 changes: 2 additions & 0 deletions RIAssigner/compute/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging

from .ComputationMethod import ComputationMethod
from .CubicSpline import CubicSpline
from .Kovats import Kovats

logging.getLogger(__name__).addHandler(logging.NullHandler())

__all__ = [
"ComputationMethod",
"CubicSpline",
"Kovats",
]
24 changes: 24 additions & 0 deletions tests/test_compute_ComputationMethod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pytest

from RIAssigner.compute import ComputationMethod, CubicSpline, Kovats


@pytest.mark.parametrize('this, other, expected', [
[CubicSpline(), CubicSpline(), True],
[Kovats(), Kovats(), True],
[CubicSpline(), Kovats(), False],
[Kovats(), object(), False],
[CubicSpline(), object(), False],
])
def test_equal(this, other, expected):
actual = (this == other)
assert actual == expected


def test_abc():
with pytest.raises(TypeError) as exception:
method = ComputationMethod()

message = exception.value.args[0]
assert exception.typename == "TypeError"
assert str(message).startswith("Can't instantiate abstract class ComputationMethod with abstract methods")