← 输入框|github.com|刷新
Skip to content

Make VersionInfo Hashable #73

Closed
Closed

Description

@s-celles

Hello,

VersionInfo doesn't implement __hash__

https://github.com/scls19fr/arduino_libraries_search/blob/master/arduino_libraries_search.py
raises TypeError: unhashable type: 'VersionInfo'

This is quite close to #72

Kind regards

Activity

  1. s-celles commented on May 13, 2018

    @s-celles
    MemberAuthor

    An other approach for hashing VersionInfo (differently than hashing it's string representation) could be to get a dict from VersionInfo but dict are not hashable (only immutable objects are hashable according https://stackoverflow.com/questions/13264511/typeerror-unhashable-type-dict).
    So we can convert dict to frozenset using

    def freeze(d):
        if isinstance(d, dict):
            return frozenset((key, freeze(value)) for key, value in d.items())
        elif isinstance(d, list):
            return tuple(freeze(value) for value in d)
        return d

    and called __hash__ of this frozenset

    Here is an example:

    In [51]: v = semver.parse_version_info('1.2.3')
    
    In [52]: d = v._asdict()
    
    In [53]: fs = freeze(d)
    
    In [54]: fs.__hash__()
    Out[54]: -1214760729421905988
  2. s-celles commented on May 13, 2018

    @s-celles
    MemberAuthor

    A last approach (and probably simpler) could be to get a tuple from a VersionInfo

    def __hash__(self):
        return hash(self._astuple())
    
  3. added a commit that references this issue on May 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    SIze

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions