Skip to content

Implement model equality#2057

Merged
visr merged 4 commits into
mainfrom
feat/model-equality
Feb 13, 2025
Merged

Implement model equality#2057
visr merged 4 commits into
mainfrom
feat/model-equality

Conversation

@evetion

@evetion evetion commented Feb 13, 2025

Copy link
Copy Markdown
Member

Fixes #928
Partially solves Deltares/Ribasim-NL#229

Differences, and such will follow in a separate PR.

@evetion evetion requested a review from visr February 13, 2025 13:51

@visr visr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great! Left a few comments, but looks good otherwise.

Comment thread python/ribasim/ribasim/input_base.py Outdated
Comment on lines +118 to +138
if self.__dict__ == other.__dict__:
return True

model_fields = type(self).__pydantic_fields__.keys()
if (
self.__dict__.keys() <= model_fields
and other.__dict__.keys() <= model_fields
):
return False

getter = (
operator.itemgetter(*model_fields)
if model_fields
else lambda _: pydantic._utils._SENTINEL # type: ignore
)
try:
return getter(self.__dict__) == getter(other.__dict__)
except KeyError:
self_fields_proxy = pydantic._utils.SafeGetItemProxy(self.__dict__) # type: ignore
other_fields_proxy = pydantic._utils.SafeGetItemProxy(other.__dict__) # type: ignore
return getter(self_fields_proxy) == getter(other_fields_proxy)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add some comments here, because I can't follow.
If their __dict__ is not equal, they can still be equal?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're reviewing Pydantic code, let's not do that. I will clarify this with a better (license) header.

Comment thread python/ribasim/tests/test_model.py
evetion and others added 2 commits February 13, 2025 16:18
Co-authored-by: Martijn Visser <mgvisser@gmail.com>
@visr visr merged commit eb81f0e into main Feb 13, 2025
@visr visr deleted the feat/model-equality branch February 13, 2025 15:53
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.

Testing model equality

2 participants