explicitly check for Document instances when dereferencing

In particular, `collections.namedtuple` instances also have a `_fields`
attribute which confuses the dereferencing.
Merge branch 'dereference-check-document-instance'