Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "TypeError: unhashable type: 'Symbol'" (and 'String'); Fix imports for Python >= 3.10 #31

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion sexpdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
]

import re
from collections import namedtuple, Iterable, Mapping, Sequence
from collections import namedtuple
try:
from collections.abc import Iterable, Mapping, Sequence
except ImportError:
# Python < 3.3
from collections import Iterable, Mapping, Sequence
from itertools import chain
from string import whitespace

Expand Down Expand Up @@ -446,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