Skip to content

Make ArchFxVariableID hashable and usable in a mapping as a key#15

Merged
haroal merged 3 commits intomainfrom
hashable-archfx-variable-id
Sep 18, 2025
Merged

Make ArchFxVariableID hashable and usable in a mapping as a key#15
haroal merged 3 commits intomainfrom
hashable-archfx-variable-id

Conversation

@haroal
Copy link
Contributor

@haroal haroal commented Sep 18, 2025

Overview & Major Changes

As part of https://github.com/iotile/archfx_cloud/pull/4614, I needed to use an ArchFxVariableID as a key in a mapping. Yet it didn't work as ArchFxVariableID does not override the default __hash__() method and so each instance of it generates a different hash value.

This PR fixes that by overriding the __hash__() method to only consider the inner "slug" value.
I've also overridden the __eq__() method to be sure we are consistent, considering that 2 ArchFXVariableID with the same slug are actually equal (instead of comparing the instance themselves).

By overriding the `__hash__()` method mainly. I've also overridden the
`__eq__()` method to be sure we are consistent, considering that 2
ArchFXVariableID with the same slug are actually equal (instead of
comparing the instance themselves).
@haroal haroal self-assigned this Sep 18, 2025
@haroal haroal added the enhancement New feature or request label Sep 18, 2025
Copy link

@Watkurem Watkurem left a comment

Choose a reason for hiding this comment

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

It would make more sense to hash the integer value of the variable, but when in Rome...

Comment on lines +44 to +47
def __eq__(self, other):
if isinstance(other, ArchFxCloudSlug):
return self._slug == other._slug
return super().__eq__(other)

Choose a reason for hiding this comment

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

The variable string is a SLUG?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahah yeah everything is a slug at the beginning, and so that's why hashing the slug directly covers all the cases at once...

@haroal haroal merged commit 8d334d0 into main Sep 18, 2025
1 check passed
@haroal haroal deleted the hashable-archfx-variable-id branch September 18, 2025 15:52
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.

2 participants