multiformats / py-multiaddr

multiaddr implementation in Python
https://multiformats.io/
Other
33 stars 23 forks source link

add __hash__ function to Multiaddr #57

Closed ShadowJonathan closed 4 years ago

ShadowJonathan commented 4 years ago

Multiaddr is currently unhashable, that means that it cannot be used in sets, dicts (as keys), frozentuples, tuples, and with much more internal logic that all depend on __hash__ functions.

Internally, Multiaddr uses a bytes variable for it's internal state, which can be hashed.

This PR adds a simple hash function that uses the hash function of those bytes, effectively making Multiaddr hashable.

Example of where this would be useful:

(self.multiaddrs is of type set)
>       self.multiaddrs.update(_multiaddr_from_socket(sock) for sock in self.server.sockets)
E       TypeError: unhashable type: 'Multiaddr'
codecov-io commented 4 years ago

Codecov Report

Merging #57 into master will increase coverage by <.01%. The diff coverage is 100%.

@@            Coverage Diff             @@
##           master      #57      +/-   ##
==========================================
+ Coverage   99.59%   99.59%   +<.01%     
==========================================
  Files          15       15              
  Lines         488      490       +2     
  Branches       66       66              
==========================================
+ Hits          486      488       +2     
  Misses          2        2