The root cause is that _attach_objects in dereference.py converts all list-like sequences to BaseList. I understand that the intent is to be able to watch for changes to the items of the list.
I'm new to Python, but it seems like tuples are meant to be the closest thing Python has to immutable data structures. It seems worth it to preserve tuple-like behaviour, even if it means losing the ability to watch for changes to these objects.
The patch below to dereference.py causes the testcase to pass. I think the best solution would be to implement BaseTuple alongside BaseList and BaseDict in base.py (looks like BasesTuple already exists?) but that's a little beyond me at this point.
diff --git a/mongoengine/dereference.py b/mongoengine/dereference.py
index 386dbf4..fcb6d89 100644
--- a/mongoengine/dereference.py
+++ b/mongoengine/dereference.py
@@ -171,6 +171,7 @@ class DeReference(object):
if not hasattr(items, 'items'):
is_list = True
+ as_tuple = isinstance(items, tuple)
iterator = enumerate(items)
data = []
else:
@@ -205,7 +206,7 @@ class DeReference(object):
if instance and name:
if is_list:
- return BaseList(data, instance, name)
+ return tuple(data) if as_tuple else BaseList(data, instance, name)
return BaseDict(data, instance, name)
depth += 1
return data