diff --git a/.travis.yml b/.travis.yml index 92845bb..24ef46d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,11 @@ language: python python: - "3.4" - "3.3" - - "3.2" - "2.7" - - "2.6" - "pypy" - "pypy3" install: + - "pip install -U pip wheel setuptools" - "pip install -r requirements-tests.txt" - "python setup.py install" script: "python setup.py nosetests && flake8 attrdict tests" diff --git a/attrdict/default.py b/attrdict/default.py index a5e5bcb..2365a20 100644 --- a/attrdict/default.py +++ b/attrdict/default.py @@ -88,8 +88,9 @@ def __repr__(self): Return a string representation of the object. """ return six.u( - "AttrDefault({default_factory}, {pass_key}, {mapping})" + "{name}({default_factory}, {pass_key}, {mapping})" ).format( + name=self.__class__.__name__, default_factory=repr(self._default_factory), pass_key=repr(self._pass_key), mapping=repr(self._mapping), diff --git a/attrdict/dictionary.py b/attrdict/dictionary.py index 874e4a4..9b5c4a0 100644 --- a/attrdict/dictionary.py +++ b/attrdict/dictionary.py @@ -45,7 +45,8 @@ def __setstate__(self, state): self._setattr('_allow_invalid_attributes', allow_invalid_attributes) def __repr__(self): - return six.u('AttrDict({contents})').format( + return six.u('{name}({contents})').format( + name=self.__class__.__name__, contents=super(AttrDict, self).__repr__() ) diff --git a/attrdict/mapping.py b/attrdict/mapping.py index 02ebac8..6f03b28 100644 --- a/attrdict/mapping.py +++ b/attrdict/mapping.py @@ -68,7 +68,8 @@ def __repr__(self): # sequence type seems like more trouble than it is worth. # If people want full serialization, they can pickle, and in # 99% of cases, sequence_type won't change anyway - return six.u("AttrMap({mapping})").format(mapping=repr(self._mapping)) + return six.u("{name}({mapping})").format(name=self.__class__.__name__, + mapping=repr(self._mapping)) def __getstate__(self): """ diff --git a/tests/test_attrdefault.py b/tests/test_attrdefault.py index dec9fc1..be68fc2 100644 --- a/tests/test_attrdefault.py +++ b/tests/test_attrdefault.py @@ -50,3 +50,30 @@ def test_repr(): ("AttrDefault(<", " 'list'>, True, {'foo': 'bar'})") ) ) + + +def test_repr_subclass(): + """ + repr(AttrDefault) + """ + from attrdict.default import AttrDefault + + class MySubClass(AttrDefault): + pass + + assert_equals(repr(MySubClass(None)), "MySubClass(None, False, {})") + + # list's repr changes between python 2 and python 3 + type_or_class = 'type' if PY2 else 'class' + + assert_equals( + repr(MySubClass(list)), + type_or_class.join(("MySubClass(<", " 'list'>, False, {})")) + ) + + assert_equals( + repr(MySubClass(list, {'foo': 'bar'}, pass_key=True)), + type_or_class.join( + ("MySubClass(<", " 'list'>, True, {'foo': 'bar'})") + ) + ) diff --git a/tests/test_attrdict.py b/tests/test_attrdict.py index b376765..5b363e3 100644 --- a/tests/test_attrdict.py +++ b/tests/test_attrdict.py @@ -104,15 +104,43 @@ def test_repr(): """ from attrdict.dictionary import AttrDict - assert_equals(repr(AttrDict()), "AttrDict({})") - assert_equals(repr(AttrDict({'foo': 'bar'})), "AttrDict({'foo': 'bar'})") - assert_equals( - repr(AttrDict({1: {'foo': 'bar'}})), "AttrDict({1: {'foo': 'bar'}})" - ) - assert_equals( - repr(AttrDict({1: AttrDict({'foo': 'bar'})})), - "AttrDict({1: AttrDict({'foo': 'bar'})})" - ) + assert_equals(repr(AttrDict()), + "AttrDict({})" + ) + + assert_equals(repr(AttrDict({'foo': 'bar'})), + "AttrDict({'foo': 'bar'})" + ) + assert_equals(repr(AttrDict({1: {'foo': 'bar'}})), + "AttrDict({1: {'foo': 'bar'}})" + ) + assert_equals(repr(AttrDict({1: AttrDict({'foo': 'bar'})})), + "AttrDict({1: AttrDict({'foo': 'bar'})})" + ) + + +def test_repr_subclass(): + """ + repr(AttrDict) + """ + + from attrdict.dictionary import AttrDict + + class MySubClass(AttrDict): + pass + + assert_equals(repr(MySubClass()), + "MySubClass({})" + ) + assert_equals(repr(MySubClass({'foo': 'bar'})), + "MySubClass({'foo': 'bar'})" + ) + assert_equals(repr(MySubClass({1: {'foo': 'bar'}})), + "MySubClass({1: {'foo': 'bar'}})" + ) + assert_equals(repr(MySubClass({1: MySubClass({'foo': 'bar'})})), + "MySubClass({1: MySubClass({'foo': 'bar'})})" + ) if not PY2: diff --git a/tests/test_attrmap.py b/tests/test_attrmap.py index c802f33..ccd980c 100644 --- a/tests/test_attrmap.py +++ b/tests/test_attrmap.py @@ -10,12 +10,38 @@ def test_repr(): """ from attrdict.mapping import AttrMap - assert_equals(repr(AttrMap()), "AttrMap({})") - assert_equals(repr(AttrMap({'foo': 'bar'})), "AttrMap({'foo': 'bar'})") - assert_equals( - repr(AttrMap({1: {'foo': 'bar'}})), "AttrMap({1: {'foo': 'bar'}})" - ) - assert_equals( - repr(AttrMap({1: AttrMap({'foo': 'bar'})})), - "AttrMap({1: AttrMap({'foo': 'bar'})})" - ) + assert_equals(repr(AttrMap()), + "AttrMap({})" + ) + assert_equals(repr(AttrMap({'foo': 'bar'})), + "AttrMap({'foo': 'bar'})" + ) + assert_equals(repr(AttrMap({1: {'foo': 'bar'}})), + "AttrMap({1: {'foo': 'bar'}})" + ) + assert_equals(repr(AttrMap({1: AttrMap({'foo': 'bar'})})), + "AttrMap({1: AttrMap({'foo': 'bar'})})" + ) + + +def test_repr_subclass(): + """ + repr(AttrMap) + """ + from attrdict.mapping import AttrMap + + class MySubClass(AttrMap): + pass + + assert_equals(repr(MySubClass()), + "MySubClass({})" + ) + assert_equals(repr(MySubClass({'foo': 'bar'})), + "MySubClass({'foo': 'bar'})" + ) + assert_equals(repr(MySubClass({1: {'foo': 'bar'}})), + "MySubClass({1: {'foo': 'bar'}})" + ) + assert_equals(repr(MySubClass({1: MySubClass({'foo': 'bar'})})), + "MySubClass({1: MySubClass({'foo': 'bar'})})" + )