Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Feat: repr() uses class name and Travis-CI fixes #46

Open
wants to merge 4 commits 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
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion attrdict/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion attrdict/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
)

Expand Down
3 changes: 2 additions & 1 deletion attrdict/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
27 changes: 27 additions & 0 deletions tests/test_attrdefault.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})")
)
)
46 changes: 37 additions & 9 deletions tests/test_attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 35 additions & 9 deletions tests/test_attrmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})})"
)