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

Conversation

hechth
Copy link
Member

@hechth hechth commented Jul 13, 2021

Implemented equality comparison in abstract base class based on class identity.

Fixes #50

@hechth hechth added the enhancement New feature or request label Jul 13, 2021
@hechth hechth requested review from martenson and xtrojak July 13, 2021 14:38
Comment on lines 22 to 23
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.

@martenson
Copy link
Member

It is unclear to me what is the intended use of this equality comparator. Could you please elaborate a bit @hechth?

@hechth
Copy link
Member Author

hechth commented Jul 15, 2021

It is unclear to me what is the intended use of this equality comparator. Could you please elaborate a bit @hechth?

The use is to be able to compare them using == in test cases. It would also be possible to move the chek to the test case and not implement the function I think.

Copy link
Member

@martenson martenson left a comment

Choose a reason for hiding this comment

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

The usage is unclear to me but it works fwiw. Be wary of what is actually being compared when using it though.

@martenson martenson merged commit 699abf8 into RECETOX:main Jul 15, 2021
@hechth hechth deleted the 50_eq_computation branch January 25, 2022 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement == operator for ComputationMethod class
3 participants