Skip to content

Commit

Permalink
Disallow []= on ImmutableDict
Browse files Browse the repository at this point in the history
Fixes #88.
  • Loading branch information
bfontaine committed Jun 24, 2024
1 parent 99a77b7 commit f128c80
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 0 additions & 5 deletions edn_format/immutable_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ def __init__(self, somedict):
def __getitem__(self, key):
return self.dict[key]

def __setitem__(self, key, value):
modifiable = dict(self.dict)
modifiable[key] = value
return ImmutableDict(modifiable)

def __repr__(self):
return self.dict.__repr__()

Expand Down
19 changes: 15 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
# TODO: Tests pass on Python 3.6, Disabled to not break tests on 2.7 :-(
# from __future__ import absolute_import, division, print_function, unicode_literals

from collections import OrderedDict
from uuid import uuid4, UUID
import random
import datetime
import fractions
import random
import unittest
from collections import OrderedDict
from uuid import uuid4, UUID

import pytz

from edn_format import edn_lex, edn_parse, \
loads, dumps, Keyword, Symbol, ImmutableDict, ImmutableList, Char, \
TaggedElement, add_tag, remove_tag, tag, \
EDNDecodeError

from edn_format.compat import _PY3, unicode


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


class ImmutableDictTest(unittest.TestCase):
def test_mutation(self):
x = ImmutableDict({})

def mutate():
nonlocal x
# noinspection PyUnresolvedReferences
x["foo"] = "bar"

self.assertRaises(TypeError, mutate)


class ImmutableListTest(unittest.TestCase):
def test_list(self):
x = ImmutableList([1, 2, 3])
Expand Down

0 comments on commit f128c80

Please sign in to comment.