Repository: marshmallow-code/marshmallow

Issue:
fields.Nested does not support sets
Currently `fields.Nested` assumes that the value of the field is a list - https://github.com/marshmallow-code/marshmallow/blob/dev/marshmallow/schema.py#L702 - and fails for `set` during serialization

Discussion:

Patch:
diff --git a/marshmallow/schema.py b/marshmallow/schema.py
index 4de0a123..7fe1289f 100644
--- a/marshmallow/schema.py
+++ b/marshmallow/schema.py
@@ -699,8 +699,8 @@ class BaseSchema(base.SchemaABC):
         """
         if obj and many:
             try:  # Homogeneous collection
-                obj_prototype = obj[0]
-            except IndexError:  # Nothing to serialize
+                obj_prototype = next(iter(obj))
+            except StopIteration:  # Nothing to serialize
                 return self.declared_fields
             obj = obj_prototype
         ret = self.dict_class()


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
index 29dada19..5d0dbbdf 100644
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -3,6 +3,7 @@
 
 import json
 import random
+from collections import namedtuple
 
 import pytest
 
@@ -687,6 +688,21 @@ def test_nested_only_and_exclude():
     assert 'bar' not in result.data['inner']
 
 
+def test_nested_with_sets():
+    class Inner(Schema):
+        foo = fields.Field()
+
+    class Outer(Schema):
+        inners = fields.Nested(Inner, many=True)
+
+    sch = Outer()
+
+    DataClass = namedtuple('DataClass', ['foo'])
+    data = dict(inners=set([DataClass(42), DataClass(2)]))
+    result = sch.dump(data)
+    assert len(result.data['inners']) == 2
+
+
 def test_meta_serializer_fields():
     u = User("John", age=42.3, email="john@example.com",
              homepage="http://john.com")