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

Use hasattr() rather than isinstance(value, MutableSequence) #31

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion booby/encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, *encoders):

@nullable
def encode(self, value):
if not isinstance(value, collections.MutableSequence):
if not hasattr(value, '__iter__'):
raise errors.EncodeError()

result = []
Expand Down
2 changes: 1 addition & 1 deletion booby/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def __init__(self, model, *args, **kwargs):
self.model = model

def __set__(self, instance, value):
if isinstance(value, collections.MutableSequence):
if hasattr(value, '__iter__'):
value = self._resolve(value)

super(Collection, self).__set__(instance, value)
Expand Down
11 changes: 8 additions & 3 deletions booby/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,19 @@ def __iter__(self):

if isinstance(value, Model):
value = dict(value)
elif isinstance(value, collections.MutableSequence):
value = self._encode_sequence(value)
if hasattr(value, '__iter__'):
value = self._encode_iterable(value)

yield name, value

def _encode_sequence(self, sequence):
def _encode_iterable(self, sequence):
result = []

if isinstance(sequence, (dict, basestring)):
# Although iterable, dictionaries are a special
# case and should be used as-is
return sequence

for value in sequence:
if isinstance(value, Model):
value = dict(value)
Expand Down
4 changes: 3 additions & 1 deletion booby/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def __init__(self, *validators):

@nullable
def validate(self, value):
if not isinstance(value, collections.MutableSequence):
# Note: Strings are iterable in python3, but we are not going
# to count strings as valid lists here.
if not hasattr(value, '__iter__') or isinstance(value, basestring):
raise errors.ValidationError('should be a list')

for i in value:
Expand Down
1 change: 0 additions & 1 deletion tests/unit/models/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class UserWithToken(User):

user = dict(UserWithToken(name='foo', email='[email protected]',
token=self.token1))

expect(user).to(have_keys(
name='foo',
email='[email protected]',
Expand Down