def test_namedtuple(self): """TODO: this fails right now because namedtuples' __new__ is overridden to accept arguments. remap's default_enter tries to create an empty namedtuple and gets a TypeError. Could make it so that immutable types actually don't create a blank new parent and instead use the old_parent as a placeholder, creating a new one at exit-time from the value's __class__ (how default_exit works now). But even then it would have to *args in the values, as namedtuple constructors don't take an iterable. """ Point = namedtuple('Point', 'x y') point_map = {'origin': [Point(0, 0)]} with pytest.raises(TypeError): remapped = remap(point_map) assert isinstance(remapped['origin'][0], Point)
# -*- coding: utf-8 -*- try: from cPickle import loads, dumps except: from pickle import loads, dumps from boltons.namedutils import namedlist, namedtuple Point = namedtuple('Point', 'x, y', rename=True) MutablePoint = namedlist('MutablePoint', 'x, y', rename=True) def test_namedlist(): p = MutablePoint(x=10, y=20) assert p == [10, 20] p[0] = 11 assert p == [11, 20] p.x = 12 assert p == [12, 20] def test_namedlist_pickle(): p = MutablePoint(x=10, y=20) assert p == loads(dumps(p)) def test_namedtuple_pickle(): p = Point(x=10, y=20) assert p == loads(dumps(p))