test_no_dereference_context_manager_object_id not working
test_no_dereference_context_manager_object_id:
https://github.com/MongoEngine/mongoengine/blob/96802599045432274481b4ed9fcc4fad4ce5f89b/tests/test_context_managers.py#L119-L154
run this test:
pytest tests/test_context_managers.py -k "test_no_dereference_context_manager_object_id"
we got no error, but if I put the assert False in with no_dereference(Group) as Group::
user = User.objects.first()
Group(ref=user, members=User.objects, generic=user).save()
with no_dereference(Group) as NoDeRefGroup:
assert False # <--------
assert Group._fields["members"]._auto_dereference
assert not NoDeRefGroup._fields["members"]._auto_dereference
pytest shows that there are still no errors!!!

I think this is because the context manager causes the error to not be caught normally
https://github.com/MongoEngine/mongoengine/blob/96802599045432274481b4ed9fcc4fad4ce5f89b/mongoengine/context_managers.py#L137-L141
returned value self.cls suppress the exception.
if i remove the return statement, rerun the test:

the pytest shows that Group._fields["members"]._auto_dereference is actually False. This is because the Group class and NoRefGroup class are the same class.
Just seeing this now, good catch!. Indeed it's due to the faulty return in exit and existing tests were actually failing. The feature was actually quite broken :grimacing: