Skip to content

Implements equality methods for impf and impfset #1027

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

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

spjuhel
Copy link
Collaborator

@spjuhel spjuhel commented Mar 18, 2025

While working on the Cost Benefit refactoring I need to test the equality between diverse climada objects,
ImpactFuncSet among other. I thought it would be worth to implement the functions as __eq__ methods.


Changes proposed in this PR:

  • Implements an __eq__ methods (==) for ImpactFunc
  • Implements an __eq__ methods (==) for ImpactFuncSet

PR Author Checklist

PR Reviewer Checklist

@spjuhel spjuhel self-assigned this Mar 18, 2025
@spjuhel spjuhel requested a review from emanuel-schmid March 18, 2025 16:44
@@ -97,6 +97,20 @@ def __init__(
self.mdd = mdd if mdd is not None else np.array([])
self.paa = paa if paa is not None else np.array([])

def __eq__(self, value: object, /) -> bool:
if isinstance(value, ImpactFunc):
Copy link
Member

Choose a reason for hiding this comment

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

I think this has the issue that derived classes won't be recognized (e.g. ImpfStormEurope).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(Which makes sense, a derived class is indeed an instance of the base class, i.e., a Square is a Rectangle, just with additional specificities :-) )

Copy link
Member

Choose a reason for hiding this comment

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

Can you please double check and make a test to be sure?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is necessary. It is the default behavior.

@peanutfun
Copy link
Member

I appreciate the effort, but this looks like boilerplate code to me. If users add new attributes, they will likely miss updating these methods. Wouldn't a simple dataclass decorator do the trick? See https://docs.python.org/3/library/dataclasses.html

@chahank
Copy link
Member

chahank commented Mar 19, 2025

What do you mean @peanutfun ? This is the impact function set class and this is about implementing the equality method.

@peanutfun
Copy link
Member

@chahank Adding @dataclass will generate this method (and others) automatically for you.

Copy link
Member

@peanutfun peanutfun left a comment

Choose a reason for hiding this comment

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

I still feel like we do not need to check the actual data inside the ImpactFunc to determine equality. But I am fine with the current approach that ensures lazy evaluation of the data arrays.

I added some suggestions for possible improvement.

Good job with the testing! It's so extensive that I almost feel like it's too much given the amount of "actual" code changes. But I also don't know which tests one could safely drop.

Notice that tests for ImpactFuncSet are still missing.

Copy link
Member

@peanutfun peanutfun left a comment

Choose a reason for hiding this comment

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

Looking very good! Two tiny nitpicks, then we are done, at least from my perspective 🙂

@@ -97,6 +97,20 @@ def __init__(
self.mdd = mdd if mdd is not None else np.array([])
self.paa = paa if paa is not None else np.array([])

def __eq__(self, value: object, /) -> bool:
if isinstance(value, ImpactFunc):
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is necessary. It is the default behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants