Skip to content

Commit 1d8daf2

Browse files
committed
Disallow []= on ImmutableDict
Fixes #88.
1 parent 11f0843 commit 1d8daf2

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

edn_format/immutable_dict.py

-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ def __init__(self, somedict):
1515
def __getitem__(self, key):
1616
return self.dict[key]
1717

18-
def __setitem__(self, key, value):
19-
modifiable = dict(self.dict)
20-
modifiable[key] = value
21-
return ImmutableDict(modifiable)
22-
2318
def __repr__(self):
2419
return self.dict.__repr__()
2520

tests.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22
# TODO: Tests pass on Python 3.6, Disabled to not break tests on 2.7 :-(
33
# from __future__ import absolute_import, division, print_function, unicode_literals
44

5-
from collections import OrderedDict
6-
from uuid import uuid4, UUID
7-
import random
85
import datetime
96
import fractions
7+
import random
108
import unittest
9+
from collections import OrderedDict
10+
from uuid import uuid4, UUID
1111

1212
import pytz
1313

1414
from edn_format import edn_lex, edn_parse, \
1515
loads, dumps, Keyword, Symbol, ImmutableDict, ImmutableList, Char, \
1616
TaggedElement, add_tag, remove_tag, tag, \
1717
EDNDecodeError
18-
1918
from edn_format.compat import _PY3, unicode
2019

2120

@@ -632,6 +631,18 @@ def test_equality(self):
632631
self.assertTrue(Symbol("db/id") == Symbol("db/id"))
633632

634633

634+
class ImmutableDictTest(unittest.TestCase):
635+
def test_mutation(self):
636+
x = ImmutableDict({})
637+
638+
def mutate():
639+
nonlocal x
640+
# noinspection PyUnresolvedReferences
641+
x["foo"] = "bar"
642+
643+
self.assertRaises(TypeError, mutate)
644+
645+
635646
class ImmutableListTest(unittest.TestCase):
636647
def test_list(self):
637648
x = ImmutableList([1, 2, 3])

0 commit comments

Comments
 (0)