diff --git a/CHANGES.rst b/CHANGES.rst index 0b6cf37..f398448 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -13,6 +13,7 @@ Version 0.10 **Changes:** - Fixed GraceEffect.durationTime `#34 `_. +- Fixed hashing `#31 `_. Version 0.9.2 diff --git a/guitarpro/models.py b/guitarpro/models.py index 46f2903..74d64bb 100644 --- a/guitarpro/models.py +++ b/guitarpro/models.py @@ -45,6 +45,11 @@ def __eq__(self, other): return self._value_ == other._value_ return super().__eq__(other) + def __hash__(self): + if self._name_ == 'unknown': + return hash(self._value_) + return hash(self._name_) + def hashableAttrs(cls=None, repr=True): """A fully hashable attrs decorator. @@ -1132,16 +1137,13 @@ class Chord: ninth: Optional['ChordAlteration'] = None eleventh: Optional['ChordAlteration'] = None firstFret: Optional[int] = None - strings: List[int] = attr.Factory(list) + strings: List[int] = attr.Factory(lambda self: [-1] * self.length, takes_self=True) barres: List['Barre'] = attr.Factory(list) omissions: List[bool] = attr.Factory(list) fingerings: List[Fingering] = attr.Factory(list) show: Optional[bool] = None newFormat: Optional[bool] = None - def __attrs_post_init__(self): - self.strings = [-1] * self.length - @property def notes(self): return [string for string in self.strings if string >= 0] diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 0026e90..af0bf51 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -56,6 +56,7 @@ def testReadWriteEquals(tmpdir, filename): gp.write(songA, destpath) songB = gp.parse(destpath) assert songA == songB + assert hash(songA) == hash(songB) @pytest.mark.parametrize('source, targetExt', CONVERSION_TESTS)