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

Disallow []= on ImmutableDict #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading