Skip to content

Commit

Permalink
Fix TypeError: unhashable type: 'Symbol' (and 'String')
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitz committed Oct 20, 2022
1 parent b59cd05 commit ecb95df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sexpdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,14 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
"""
>>> D = {'a': 1, String('a'): 2, Symbol('a'): 3}
>>> len(D)
3
"""
return unicode.__hash__(self)

_lisp_quoted_specials = [ # from Pymacs
('\\', '\\\\'), # must come first to avoid doubly quoting "\"
('"', '\\"'), ('\b', '\\b'), ('\f', '\\f'),
Expand Down
11 changes: 11 additions & 0 deletions test_sexpdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def test_parse_special_symbols(self):
r'\ ', r'\.', r'\,', r'\?', r'\;', r'\#']:
self.assert_parse(s, Symbol(Symbol.unquote(s)))

def test_hashable_and_distinct(self):
d = {
String("A"): "StrA",
Symbol("A"): "SymA",
"A": "strA",
}
self.assertEqual(3, len(d))
self.assertEqual("StrA", d[String("A")])
self.assertEqual("SymA", d[Symbol("A")])
self.assertEqual("strA", d["A"])


class TestParseFluctuation(BaseTestCase):

Expand Down

0 comments on commit ecb95df

Please sign in to comment.